/
encode.go
62 lines (47 loc) · 898 Bytes
/
encode.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
package main
import (
"bytes"
"fmt"
"image"
"image/gif"
"image/jpeg"
"io/ioutil"
"os"
"path"
)
func main() {
out, err := os.Create("./output.gif")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dir, _ := ioutil.ReadDir(".")
var imgs []*image.Paletted
for _, file := range dir {
switch ext := path.Ext(file.Name()); ext {
case ".jpg":
f, _ := os.Open(file.Name())
img, _ := jpeg.Decode(f)
var opt gif.Options
opt.NumColors = 256
buf := new(bytes.Buffer)
gif.Encode(buf, img, &opt)
g, _ := gif.DecodeAll(buf)
imgs = append(imgs, g.Image[0])
default:
fmt.Println(ext)
}
}
g := new(gif.GIF)
g.Image = imgs
var opt gif.Options
opt.NumColors = 256
g.Delay = []int{100, 100, 100}
g.LoopCount = 1000
err = gif.EncodeAll(out, g)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Generated image to output.gif \n")
}