You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.4 KiB
68 lines
1.4 KiB
package main
|
|
|
|
import (
|
|
"os"
|
|
"image"
|
|
"log"
|
|
"image/png"
|
|
"github.com/disintegration/gift"
|
|
"flag"
|
|
"lcthw.dev/go/jankifier/filters"
|
|
)
|
|
|
|
func LoadImage(filename string) image.Image {
|
|
reader, err := os.Open(filename)
|
|
if err != nil { log.Fatal(err) }
|
|
defer reader.Close()
|
|
|
|
img, _, err := image.Decode(reader)
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
return img
|
|
}
|
|
|
|
func SaveImage(filename string, img image.Image) {
|
|
out, err := os.Create(filename)
|
|
if err != nil {
|
|
log.Fatalf("can't write file %s: %v", filename, err)
|
|
}
|
|
defer out.Close()
|
|
|
|
err = png.Encode(out, img)
|
|
if err != nil {
|
|
log.Fatalf("can't png encode %s: %v", filename, err)
|
|
}
|
|
}
|
|
|
|
type Opts struct {
|
|
InFile string
|
|
OutFile string
|
|
PixelWidth int
|
|
}
|
|
|
|
func ParseOpts() Opts {
|
|
var opts Opts
|
|
|
|
flag.StringVar(&opts.InFile, "input", "", "input file.png")
|
|
flag.StringVar(&opts.OutFile, "output", "", "output file.png")
|
|
flag.IntVar(&opts.PixelWidth, "pixel-width", 4, "pixel width")
|
|
flag.Parse()
|
|
|
|
return opts
|
|
}
|
|
|
|
func main() {
|
|
opts := ParseOpts()
|
|
|
|
src := LoadImage(opts.InFile)
|
|
bounds := src.Bounds()
|
|
|
|
resize := gift.Resize(bounds.Max.X / opts.PixelWidth, 0, gift.NearestNeighborResampling)
|
|
upscale := filters.UpscaleImage(bounds, opts.PixelWidth)
|
|
|
|
g := gift.New(resize, upscale)
|
|
smaller := image.NewNRGBA(g.Bounds(bounds))
|
|
g.Draw(smaller, src)
|
|
|
|
SaveImage(opts.OutFile, smaller)
|
|
}
|
|
|