Created
December 13, 2013 20:14
-
-
Save imjasonh/7950600 to your computer and use it in GitHub Desktop.
Resize a PNG image, optionally delineating individual pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"image" | |
"image/color" | |
"image/png" | |
"log" | |
"os" | |
) | |
var in = flag.String("in", "", "File to resize") | |
var x = flag.Int("x", 5, "Factor to resize") | |
var del = flag.Bool("d", false, "Whether to deliniate pixels") | |
func main() { | |
flag.Parse() | |
inf, err := os.Open(*in) | |
if err != nil { | |
log.Fatal(err) | |
} | |
i, err := png.Decode(inf) | |
if err != nil { | |
log.Fatal(err) | |
} | |
r := resized{i, *x, *del} | |
newf, err := os.Create("big-" + *in) | |
if err != nil { | |
log.Fatal(err) | |
} | |
png.Encode(newf, r) | |
} | |
type resized struct { | |
i image.Image | |
factor int | |
deliniate bool | |
} | |
func (r resized) ColorModel() color.Model { | |
return r.i.ColorModel() | |
} | |
func (r resized) Bounds() image.Rectangle { | |
return image.Rectangle{r.i.Bounds().Min, image.Point{ | |
X: r.i.Bounds().Max.X * r.factor, | |
Y: r.i.Bounds().Max.Y * r.factor, | |
}} | |
} | |
func (r resized) At(x, y int) color.Color { | |
if r.deliniate && (x%r.factor == 0 || y%r.factor == 0) { | |
return color.Gray{0} | |
} | |
return r.i.At(x/r.factor, y/r.factor) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment