davidbyttow / govips

A lightning fast image processing and resizing library for Go
MIT License
1.31k stars 203 forks source link

Want to generate animated webp but result is long image #449

Closed TechMaster closed 1 month ago

TechMaster commented 1 month ago

I want to scan all images in a folder inputDir then join them then export to webp. Following code can compile but generate long image rather than animated webp.

Please help to export to animated webp correctly. Thanks

func GenerateAnimatedWebP(inputDir string, outputDir string) {
    // Load images as frames from inputDir
    files, err := os.ReadDir(inputDir)
    if err != nil {
        log.Fatalf("failed to read directory %s: %v", inputDir, err)
    }

    var imageRefs []*vips.ImageRef

    for _, file := range files {
        if !file.IsDir() {
            ext := strings.ToLower(filepath.Ext(file.Name()))
            if ext == ".jpg" || ext == ".png" {
                framePath := filepath.Join(inputDir, file.Name())
                img, err := vips.NewImageFromFile(framePath)
                if err != nil {
                    log.Fatalf("failed to load image %s: %v", framePath, err)
                }
                defer img.Close()
                imageRefs = append(imageRefs, img)
            }
        }
    }

    // Join images into a single image
    joinedImage := imageRefs[0] // Start with the first image

    if err != nil {
        log.Fatalf("failed to initialize joined image: %v", err)
    }
    defer joinedImage.Close()

    joinedImage.SetPageHeight(imageRefs[0].Height())
    err = joinedImage.ArrayJoin(imageRefs[1:], 1) // Join images vertically
    if err != nil {
        log.Fatalf("failed to join images: %v", err)
    }

    // Set delay for each frame
    delays := make([]int, len(imageRefs))
    for i := range delays {
        delays[i] = 100
    }
    joinedImage.SetPageDelay(delays)
    // Save the joined image as an animated WebP
    options := vips.WebpExportParams{
        Quality:  50,
        Lossless: false,
    }

    webpData, _, err := joinedImage.ExportWebp(&options)
    if err != nil {
        log.Fatalf("failed to export webp: %v", err)
    }

    // Save the animated WebP to file
    outputFilePath := filepath.Join(outputDir, "output.webp")
    err = os.WriteFile(outputFilePath, webpData, 0644)
    if err != nil {
        log.Fatalf("failed to save webp: %v", err)
    }

    fmt.Println("Animated WebP created successfully: output.webp")
}
tonimelisma commented 1 month ago

Hey @TechMaster. GitHub issues are for reporting bugs in govips. It looks like you're looking for assistance. I refer you to the documentation and examples. The library is open source so you can dwelve into it if you wish. Unfortunately as the project is run by volunteers we don't have bandwidth to provide guidance on how to use the library outside of the mentioned documentation. You are free to contribute more or better documentation or tutorials if you wish, and it would be greatly appreciated!