diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2019-12-02 13:41:25 +0000 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2019-12-02 13:41:25 +0000 |
| commit | 2063ef42fc0ec2f729217af25a9504c820a5cb63 (patch) | |
| tree | 0944508bc4cb472b8538d46e5a62eab07a984206 | |
| parent | 1c025c8a702c7747798cada0d34722847b2367b2 (diff) | |
| download | distill-2063ef42fc0ec2f729217af25a9504c820a5cb63.tar.xz distill-2063ef42fc0ec2f729217af25a9504c820a5cb63.zip | |
Parse command line args
| -rw-r--r-- | distill.go | 39 |
1 files changed, 30 insertions, 9 deletions
@@ -10,16 +10,37 @@ import ( "github.com/EdlinOrg/prominentcolor" "github.com/anthonynsimon/bild/clone" "github.com/anthonynsimon/bild/imgio" + "github.com/jessevdk/go-flags" colorful "github.com/lucasb-eyer/go-colorful" ) var ( - blockSize = 50 - maxDominantColours = 8 + opts struct { + BlockSize int `short:"b" long:"block-size" default:"50" description:"The size of the blocks the image should be distilled to. E.g. 10 will result in square blocks of 10x10 pixels. Note that this is the size of the blocks, not the number of blocks."` + MaxDominantColours int `short:"n" long:"max-dominant-colours" default:"8" description:"The number of dominant colours to be extracted from the image."` + OutputPath string `short:"o" long:"output-path" default:"out.png" description:"Path to the output file. The format will be png."` + } + imagePath string ) +func init() { + args, err := flags.Parse(&opts) + if err != nil { + + os.Exit(1) + } + if len(args) == 1 { + imagePath = args[0] + } else { + fmt.Println("Specity an image to process") + os.Exit(1) + } + +} + func main() { - img, err := imgio.Open("starry-night.jpg") + + img, err := imgio.Open(imagePath) if err != nil { fmt.Println(err) os.Exit(1) @@ -33,18 +54,18 @@ func main() { os.Exit(1) } - for x := 0; x < source.Bounds().Max.X; x += blockSize { - for y := 0; y < source.Bounds().Max.Y; y += blockSize { + for x := 0; x < source.Bounds().Max.X; x += opts.BlockSize { + for y := 0; y < source.Bounds().Max.Y; y += opts.BlockSize { var ( - block = source.SubImage(image.Rect(x, y, x+blockSize, y+blockSize)) + block = source.SubImage(image.Rect(x, y, x+opts.BlockSize, y+opts.BlockSize)) avgBlockColour = calculateAverageBlockColour(block) nearestColour = nearestColour(avgBlockColour, prominentColours) ) - fillBlock(x, y, blockSize, nearestColour, source) + fillBlock(x, y, opts.BlockSize, nearestColour, source) } } - if err := imgio.Save("out.png", source, imgio.PNGEncoder()); err != nil { + if err := imgio.Save(opts.OutputPath, source, imgio.PNGEncoder()); err != nil { fmt.Println(err) os.Exit(1) } @@ -62,7 +83,7 @@ func extractProminentColours(image image.Image) ([]colorful.Color, error) { var prominentcolors []colorful.Color sampleWidth := uint(image.Bounds().Max.X / 2) - colours, err := prominentcolor.KmeansWithAll(maxDominantColours, image, prominentcolor.ArgumentNoCropping, sampleWidth, nil) + colours, err := prominentcolor.KmeansWithAll(opts.MaxDominantColours, image, prominentcolor.ArgumentNoCropping, sampleWidth, nil) if err != nil { return nil, err } |