generate sprite images in grid to fix tagger sprite view

This commit is contained in:
cacheflush 2026-01-27 15:56:56 +01:00
parent f2e8bab58a
commit 25c5fb20fc
2 changed files with 26 additions and 8 deletions

View file

@ -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 {

View file

@ -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))))
}