-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (53 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"image"
"image/color/palette"
"image/gif"
"image/png"
"math/rand"
"os"
)
func writeGifFile(outFileName string, g gif.GIF) {
outFile, _ := os.Create(outFileName)
defer outFile.Close()
g.Delay = make([]int, len(g.Image))
g.LoopCount = 0
gif.EncodeAll(outFile, &g)
}
func slideImage(in image.Image, slideX int, slideY int) *image.Paletted {
margin := 2
inBounds := in.Bounds()
rect := image.Rect(inBounds.Min.X, inBounds.Min.Y, inBounds.Max.X+margin, inBounds.Max.Y+margin)
pl := image.NewPaletted(rect, palette.WebSafe)
for x := 0; x < inBounds.Max.X; x++ {
for y := 0; y < inBounds.Max.Y; y++ {
pl.Set(x+slideX, y+slideY, in.At(x, y))
}
}
return pl
}
func calsSlideVolume() (int, int) {
x := (rand.Int() % 3)
y := (rand.Int() % 3)
return x, y
}
func generateAnimeGif(inFileName string) {
inFile, _ := os.Open(inFileName)
defer inFile.Close()
pngImage, _ := png.Decode(inFile)
moveTimes := 100
outGif := gif.GIF{
Image: []*image.Paletted{},
}
for s := 0; s < moveTimes; s++ {
palet := slideImage(pngImage, 1, 1)
outGif.Image = append(outGif.Image, palet)
slideX, slideY := calsSlideVolume()
palet = slideImage(pngImage, slideX, slideY)
outGif.Image = append(outGif.Image, palet)
}
writeGifFile(inFileName+".gif", outGif)
}
func main() {
generateAnimeGif(os.Args[1])
}