-
Notifications
You must be signed in to change notification settings - Fork 0
/
glitch.go
116 lines (92 loc) · 2.28 KB
/
glitch.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
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bytes"
"fmt"
"github.com/jpoz/glitch"
"image"
"image/gif"
"log"
"math/rand"
"os"
"time"
)
func createGlitch(filename string) *glitch.Glitch {
gl, err := glitch.NewGlitch(filename)
if err != nil {
log.Fatalf("could not create new glitch: %v", err)
}
return gl
}
func start(filename string, iterations uint) {
gl := createGlitch(filename)
generate(gl, filename, iterations)
}
func generate(gl *glitch.Glitch, filename string, iterations uint) {
file, ext, directory := getFileDetails(filename)
// Directory
if _, err := os.Stat(directory); os.IsNotExist(err) {
os.MkdirAll(directory, os.ModePerm)
}
files := make([]string, 0)
for i := uint(0); i < iterations; i++ {
rand.Seed(time.Now().UnixNano())
gl.Seed(rand.Int63())
gl.Copy()
glitchIt(gl)
filename := createFileName(file, ext, i)
fmt.Printf("[%d/%d] Generating %s\n", i+1, iterations, filename)
f, err := os.Create(filename)
if err != nil {
panic("could not create new file")
}
gl.Write(f)
files = append(files, filename)
// move
err = os.Rename(filename, fmt.Sprintf("%s/%s", directory, filename))
if err != nil {
log.Fatal(err)
}
}
createAnimation(files, directory, file)
}
func createAnimation(files []string, directory, filename string) {
fmt.Println("Generating GIF")
outGif := &gif.GIF{}
total := len(files)
for i, name := range files {
fmt.Printf("[%d/%d] Processing GIF\n", i+1, total)
input := fmt.Sprintf("%s/%s", directory, name)
f, err := os.Open(input)
if err != nil {
log.Fatal(err)
}
imageData, _, err := image.Decode(f)
if err != nil {
log.Println("Error al decorde archivo")
log.Fatal(err)
}
buf := bytes.Buffer{}
if err = gif.Encode(&buf, imageData, nil); err != nil {
log.Println("Error al encode archivo")
log.Fatal(err)
}
inGif, err := gif.Decode(&buf)
if err != nil {
log.Println("Erro en gif decode")
log.Fatal(err)
}
f.Close()
outGif.Image = append(outGif.Image, inGif.(*image.Paletted))
outGif.Delay = append(outGif.Delay, 0)
}
output := fmt.Sprintf("FINAL_%s.gif", createGifName(filename))
f, err := os.Create(output)
if err != nil {
log.Fatal(err)
}
gif.EncodeAll(f, outGif)
err = os.Rename(output, fmt.Sprintf("%s/%s", directory, output))
if err != nil {
log.Fatal(err)
}
}