forked from yurivish/pix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pix.go
83 lines (74 loc) · 1.61 KB
/
pix.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
package pix
import (
"image/png"
"os"
"os/exec"
"path/filepath"
"strconv"
)
type Options struct {
Width, Height int
RandomSeed int64
Sort SortOptions
Seeds []int
Output string
CompressionLevel png.CompressionLevel
Video bool
}
func Place(colors []SampledColor, opts Options) error {
// Create a canvas object
canvas := NewCanvas(opts.Width, opts.Height, opts.RandomSeed)
// Place an initial seed color in the middle of the canvas
seeds := opts.Seeds
if seeds == nil {
seeds = []int{opts.Width / 2, opts.Height / 2}
}
rest, err := canvas.PlaceSeeds(colors, seeds...)
if err != nil {
return err
}
i := 1
frame := 1
// Place the rest of the colors using the growth algorithm
for _, color := range rest {
canvas.Place(color)
if opts.Video {
if i%200 == 1 || i+1 == opts.Height*opts.Width {
err = canvas.SaveImage("progress/"+strconv.Itoa(frame)+".png", opts.CompressionLevel)
if err != nil {
return err
}
frame++
}
}
i++
}
cmd := exec.Command("ffmpeg",
"-y",
"-start_number", "1",
"-i", "progress/%d.png",
"-pix_fmt", "yuv420p",
"-movflags", "+faststart",
"-vcodec", "libx264",
"./progress/progress.mp4")
err = cmd.Run()
if err != nil {
return err
}
files, err := filepath.Glob("progress/*.png")
if err != nil {
return err
}
for _, f := range files {
if err := os.Remove(f); err != nil {
return err
}
}
// Save the output image
outPath := opts.Output
if outPath == "" {
outPath = "out.png"
}
// fmt.Println("saving", outPath)
return canvas.SaveImage(outPath, opts.CompressionLevel)
}