sunshineplan/imgconv

How to convert gif to jpg per second?

Closed this issue · 3 comments

How to convert gif to jpg per second?

Thank you!

convert each frame of a gif to a jpg.

imgconv currently does not support multi-image single-file formats. You can first extract all the images using the official package and then convert them.

package main

import (
	"fmt"
	"image/gif"
	"image/jpeg"
	"log"
	"os"

	"github.com/sunshineplan/imgconv"
)

func main() {
	// Open the GIF file
	f, err := os.Open("input.gif")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	// Decode the GIF file
	gifImg, err := gif.DecodeAll(f)
	if err != nil {
		log.Fatal(err)
	}

	// Loop through each frame of the GIF and save it as a JPEG image
	for i, frame := range gifImg.Image {
		outFile, err := os.Create(fmt.Sprintf("frame%d.jpg", i))
		if err != nil {
			log.Fatal(err)
		}
		defer outFile.Close()

		if err := imgconv.Write(outFile, frame, &imgconv.FormatOption{Format: imgconv.JPEG}); err != nil {
			log.Fatal(err)
		}
		// or
		if err := jpeg.Encode(outFile, frame, nil); err != nil {
			log.Fatal(err)
		}
	}
}

Thanks, the gif.DecodeAll() worked!