Skip to content

Instantly share code, notes, and snippets.

@jiro4989
Created August 13, 2023 13:57
Show Gist options
  • Save jiro4989/362a656c78267497dd60c9f632c64aa5 to your computer and use it in GitHub Desktop.
Save jiro4989/362a656c78267497dd60c9f632c64aa5 to your computer and use it in GitHub Desktop.
Go言語のcutterライブラリを使って、画像をトリミングしてアニメーションGIFとして出力する
package main
import (
"image"
"image/color"
"image/color/palette"
"image/draw"
"image/gif"
"os"
"github.com/oliamb/cutter"
)
func main() {
rect := image.Rect(0, 0, 100, 100)
rgb := image.NewRGBA(rect)
for x := 10; x < 20; x++ {
for y := 10; y < 20; y++ {
rgb.Set(x, y, color.RGBA{R: 255, A: 255})
}
}
c, err := cutter.Crop(rgb, cutter.Config{
Width: 20,
Height: 20,
Anchor: image.Pt(0, 0),
})
if err != nil {
panic(err)
}
img := c
b := c.Bounds()
ps := make([]*image.Paletted, 0)
p := image.NewPaletted(b, palette.Plan9)
draw.Draw(p, p.Rect, img, b.Min, draw.Over)
ps = append(ps, p)
f, err := os.Create("sushi.gif")
if err != nil {
panic(err)
}
defer f.Close()
err = gif.EncodeAll(f, &gif.GIF{
Image: ps,
Delay: []int{1},
})
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment