Created
August 13, 2023 13:57
-
-
Save jiro4989/362a656c78267497dd60c9f632c64aa5 to your computer and use it in GitHub Desktop.
Go言語のcutterライブラリを使って、画像をトリミングしてアニメーションGIFとして出力する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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