-
Notifications
You must be signed in to change notification settings - Fork 4
/
gif2img.go
103 lines (93 loc) · 2.1 KB
/
gif2img.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// gif2img
// thx: https://gist.github.com/linw1995/820b05ac3fbab34937635d30a61740fb
package main
import (
"flag"
"fmt"
"image"
"image/draw"
"image/gif"
"image/png"
"log"
"os"
"strings"
)
var (
flagHelp = flag.Bool("h", false, "Shows usage options.")
flagTarget = flag.String("f", "", "choose gif file")
)
func banner() {
t := `
_ ___ ______ _
(_) / __|_____ \(_)
____ _ _| |__ ____) )_ ____ ____
/ _ | (_ __)/ ____/| | \ / _ |
( (_| | | | | | (_____| | | | ( (_| |
\___ |_| |_| |_______)_|_|_|_|\___ |
(_____| (_____|
`
fmt.Println(t)
}
func main() {
banner()
flag.Parse()
if *flagHelp || *flagTarget == "" {
fmt.Printf("Usage: gif2img [options]\n\n")
flag.PrintDefaults()
return
}
inGif, err := decodeGif(*flagTarget)
if err != nil {
log.Fatalln(err)
}
f, err := os.Open(*flagTarget)
if err != nil {
log.Fatalf("open '%s' error: %s", *flagTarget, err)
}
defer f.Close()
config, err := gif.DecodeConfig(f)
if err != nil {
fmt.Printf("decode gif config '%s' error: %s", *flagTarget, err)
return
}
rect := image.Rect(0, 0, config.Width, config.Height)
if rect.Min == rect.Max {
var max image.Point
for _, frame := range inGif.Image {
maxF := frame.Bounds().Max
if max.X < maxF.X {
max.X = maxF.X
}
if max.Y < maxF.Y {
max.Y = maxF.Y
}
}
rect.Max = max
}
format := fmt.Sprintf("%s-%%d.png", strings.Split(*flagTarget, ".")[0])
for i, srcimg := range inGif.Image {
img := image.NewRGBA(rect)
subfn := fmt.Sprintf(format, i)
f, err := os.Create(subfn)
if err != nil {
fmt.Printf("create file '%s' error: %s", subfn, err)
return
}
draw.Draw(img, img.Bounds(), srcimg, srcimg.Rect.Min, draw.Src)
png.Encode(f, img)
f.Close()
fmt.Printf("[+] '%s' ok...\n", subfn)
}
}
func decodeGif(fn string) (*gif.GIF, error) {
f, err := os.Open(fn)
if err != nil {
return nil, fmt.Errorf("open '%s' error: %s", fn, err)
}
defer f.Close()
Gif, err := gif.DecodeAll(f)
if err != nil {
return nil, fmt.Errorf("decode '%s' error: %s", fn, err)
}
return Gif, nil
}