Guest User

cover-collage

a guest
Sep 18th, 2025
149
0
357 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.21 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "image"
  6.     "image/draw"
  7.     "image/jpeg"
  8.     "math"
  9.     "os"
  10.     "path/filepath"
  11.     "strings"
  12.     "github.com/nfnt/resize"
  13.     "github.com/urfave/cli"
  14. )
  15.  
  16. func getImagePaths(directory string) ([]string, error) {
  17.     var imagePaths []string
  18.     err := filepath.Walk(directory, func(path string, fi os.FileInfo, err error) error {
  19.         if err != nil {
  20.             return err
  21.         }
  22.  
  23.         if fi.IsDir() {
  24.             return nil
  25.         }
  26.  
  27.         if strings.HasSuffix(path, "cover.jpg") ||
  28.            strings.HasSuffix(path, "Cover.jpg") ||
  29.            strings.HasSuffix(path, "folder.jpg") ||
  30.            strings.HasSuffix(path, "front.jpg") {
  31.             imagePaths = append(imagePaths, path)
  32.         }
  33.  
  34.         return nil
  35.     })
  36.  
  37.     if err != nil {
  38.         return nil, err
  39.     }
  40.  
  41.     return imagePaths, nil
  42. }
  43.  
  44. func generateCollage(imagePaths []string, size int, columns int) {
  45.     rows := int(math.Ceil(float64(len(imagePaths)) / float64(columns)))
  46.     canvas := image.NewRGBA(image.Rect(0, 0, columns * size, rows * size))
  47.  
  48.     curCol, curRow := 0, 0
  49.     for _, imagePath := range imagePaths {
  50.         imageFile, err := os.Open(imagePath)
  51.         if err != nil {
  52.             panic(err)
  53.         }
  54.  
  55.         img, _, err := image.Decode(imageFile)
  56.         if err != nil {
  57.             panic(err)
  58.         }
  59.  
  60.         scaledImg := resize.Resize(uint(size), 0, img, resize.Lanczos3)
  61.  
  62.         sp := image.Point{curCol * size, curRow * size}
  63.         rect := image.Rectangle{sp, sp.Add(image.Point{size, size})}
  64.         draw.Draw(canvas, rect.Bounds(), scaledImg, image.Point{0, 0}, draw.Src)
  65.  
  66.         curCol = curCol + 1
  67.         if curCol >= columns {
  68.             curCol = 0
  69.             curRow = curRow + 1
  70.         }
  71.     }
  72.  
  73.     out, err := os.Create("out.jpg")
  74.     if err != nil {
  75.         panic(err)
  76.     }
  77.  
  78.     jpeg.Encode(out, canvas, &jpeg.Options{96})
  79.     out.Close()
  80. }
  81.  
  82. func main() {
  83.     app := cli.NewApp()
  84.     app.Name = "cover-collage"
  85.     app.Version = "0.0.1"
  86.  
  87.     app.Flags = []cli.Flag{
  88.         cli.IntFlag{Name: "size, s", Value: 120},
  89.         cli.IntFlag{Name: "columns, c", Value: 8},
  90.     }
  91.  
  92.     app.Action = func(ctx *cli.Context) error {
  93.         if len(ctx.Args()) < 1 {
  94.             fmt.Println("Incorrect Usage.\n")
  95.             cli.ShowAppHelp(ctx)
  96.             return nil
  97.         }
  98.  
  99.         directory := ctx.Args()[0]
  100.         imagePaths, _ := getImagePaths(directory)
  101.         generateCollage(imagePaths, ctx.Int("size"), ctx.Int("columns"))
  102.  
  103.         return nil
  104.     }
  105.  
  106.     app.Run(os.Args)
  107. }
  108.  
Add Comment
Please, Sign In to add comment