Skip to content

Commit

Permalink
support %d, %03d, etc. in output filename to save frames
Browse files Browse the repository at this point in the history
  • Loading branch information
fogleman committed Sep 21, 2016
1 parent 33d0d61 commit 9caadaa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
44 changes: 29 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -20,7 +21,7 @@ var (
InputSize int
OutputSize int
Mode int
V, VV, VVV bool
V, VV bool
)

func init() {
Expand All @@ -33,7 +34,6 @@ func init() {
flag.IntVar(&Mode, "m", 1, "0=combo 1=triangle 2=rect 3=ellipse 4=circle 5=rotatedrect")
flag.BoolVar(&V, "v", false, "verbose")
flag.BoolVar(&VV, "vv", false, "very verbose")
// flag.BoolVar(&VVV, "vvv", false, "very very verbose")
}

func errorMessage(message string) bool {
Expand Down Expand Up @@ -67,9 +67,6 @@ func main() {
if VV {
primitive.LogLevel = 2
}
if VVV {
primitive.LogLevel = 3
}

// seed random number generator
rand.Seed(time.Now().UTC().UnixNano())
Expand All @@ -85,18 +82,35 @@ func main() {
size := uint(InputSize)
input = resize.Thumbnail(size, size, input, resize.Bilinear)

// determine output options
ext := strings.ToLower(filepath.Ext(Output))
saveFrames := strings.Contains(Output, "%") && ext != ".gif"

This comment has been minimized.

Copy link
@ropery

ropery Sep 23, 2016

👎


// run algorithm
model := primitive.NewModel(input, Alpha, OutputSize, primitive.Mode(Mode))
output := model.Run(Number)
start := time.Now()
for i := 1; i <= Number; i++ {
// find optimal shape and add it to the model
model.Step()
elapsed := time.Since(start).Seconds()
primitive.Log(1, "iteration %d, time %.3f, score %.6f\n", i, elapsed, model.Score)

// write output image
primitive.Log(1, "writing %s\n", Output)
if strings.HasSuffix(strings.ToLower(Output), ".gif") {
frames := model.Frames(0.001)
primitive.SaveGIFImageMagick(Output, frames, 50, 250)
} else if strings.HasSuffix(strings.ToLower(Output), ".svg") {
primitive.SaveFile(Output, model.SVG())
} else {
primitive.SavePNG(Output, output)
// write output image(s)
if saveFrames || i == Number {
path := Output
if saveFrames {
path = fmt.Sprintf(Output, i)
}
primitive.Log(1, "writing %s\n", Output)
switch ext {
case ".png":
primitive.SavePNG(path, model.Context.Image())
case ".svg":
primitive.SaveFile(path, model.SVG())
case ".gif":
frames := model.Frames(0.001)
primitive.SaveGIFImageMagick(path, frames, 50, 250)
}
}
}
}
27 changes: 9 additions & 18 deletions primitive/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"github.com/fogleman/gg"
)

const (
SaveFrames = false
OutlineShapes = false
)

type Model struct {
W, H int
Background color.Color
Expand Down Expand Up @@ -135,20 +130,16 @@ func (model *Model) Run(n int) image.Image {
model.Step()
elapsed := time.Since(start).Seconds()
v("iteration %d, time %.3f, score %.6f\n", i, elapsed, model.Score)
if SaveFrames {
SavePNG("out.png", model.Current)
model.Context.SavePNG(fmt.Sprintf("out%03d.png", i))
}
}
if OutlineShapes {
for _, shape := range model.Shapes {
model.Context.NewSubPath()
shape.Draw(model.Context)
}
c := averageImageColor(model.Target)
model.Context.SetRGBA255(int(c.R), int(c.G), int(c.B), 64)
model.Context.Stroke()
}
// if OutlineShapes {
// for _, shape := range model.Shapes {
// model.Context.NewSubPath()
// shape.Draw(model.Context)
// }
// c := averageImageColor(model.Target)
// model.Context.SetRGBA255(int(c.R), int(c.G), int(c.B), 64)
// model.Context.Stroke()
// }
return model.Context.Image()
}

Expand Down

1 comment on commit 9caadaa

@ropery
Copy link

@ropery ropery commented on 9caadaa Sep 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convenient, but overloading the -o option like this is bad.
Simply the presence of % in the filename given to -o triggers saving frames... that is very unexpected and undocumented.
So please make saving frames another option.

Please sign in to comment.