From 25c5fb20fcb88ef30874da62f00a7c74e4251bfc Mon Sep 17 00:00:00 2001 From: cacheflush Date: Tue, 27 Jan 2026 15:56:56 +0100 Subject: [PATCH] generate sprite images in grid to fix tagger sprite view --- pkg/scene/generate/sprite.go | 27 +++++++++++++++++++-------- pkg/utils/image.go | 7 +++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/pkg/scene/generate/sprite.go b/pkg/scene/generate/sprite.go index 663633cd7..dc8be5e52 100644 --- a/pkg/scene/generate/sprite.go +++ b/pkg/scene/generate/sprite.go @@ -66,12 +66,17 @@ func (g Generator) CombineSpriteImages(images []image.Image) image.Image { // Combine all of the thumbnails into a sprite image width := images[0].Bounds().Size().X height := images[0].Bounds().Size().Y - canvasWidth := width * len(images) - canvasHeight := height + gridSize := utils.GetGridSizeFromImageCount(len(images)) + canvasWidth := width * gridSize + canvasHeight := height * gridSize montage := imaging.New(canvasWidth, canvasHeight, color.NRGBA{}) + row := 0 for index := 0; index < len(images); index++ { - x := width * index - y := 0 + if (index > 0) && (index%gridSize == 0) { + row++ + } + x := width * (index % gridSize) + y := height * row img := images[index] montage = imaging.Paste(montage, img, image.Pt(x, y)) } @@ -97,13 +102,19 @@ func (g Generator) spriteVTT(spritePath string, stepSize float64, video_duration if err != nil { return err } - width := image.Width / number_of_sprites - height := image.Height + + gridSize := utils.GetGridSizeFromImageCount(number_of_sprites) + width := image.Width / gridSize + height := image.Height / gridSize vttLines := []string{"WEBVTT", ""} + row := 0 for index := 0; index < number_of_sprites; index++ { - x := width * index - y := 0 + if (index > 0) && (index%gridSize == 0) { + row++ + } + x := width * (index % gridSize) + y := height * row startTime := float64(index) * stepSize endTime := float64(index+1) * stepSize if endTime > video_duration { diff --git a/pkg/utils/image.go b/pkg/utils/image.go index a5d9bb1f1..8bb64060e 100644 --- a/pkg/utils/image.go +++ b/pkg/utils/image.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "fmt" "io" + "math" "net/http" "regexp" "time" @@ -120,3 +121,9 @@ func ServeImage(w http.ResponseWriter, r *http.Request, image []byte) { w.Header().Set("Content-Type", contentType) ServeStaticContent(w, r, image) } + +// GetGridSizeFromImageCount return the required size of a grid, where the number of images in width +// equals the number of images in height, to hold 'imageCount' images +func GetGridSizeFromImageCount(imageCount int) int { + return int(math.Ceil(math.Sqrt(float64(imageCount)))) +}