Last active
August 29, 2015 14:00
-
-
Save apg/3d087337715e41c4b9bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
for n in {10..85}; do | |
./pixmaker -s $n -src ~/heartbleed.png ~/frame$n.png | |
done | |
gifme --reverse ~/Desktop/frame*.png |
This file contains hidden or 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/draw" | |
"log" | |
"os" | |
"image/png" | |
) | |
func dominate(im image.Image, r image.Rectangle) color.Color { | |
colors := make(map[color.Color]int) | |
for y := r.Min.Y; y < r.Max.Y; y++ { | |
for x := r.Min.X; x < r.Max.X; x++ { | |
c := im.At(x, y) | |
if v, exists := colors[c]; exists { | |
colors[c] = v + 1 | |
} else { | |
colors[c] = 1 | |
} | |
} | |
} | |
var color color.Color | |
max := -1 | |
for k, v := range colors { | |
if v > max { | |
color = k | |
max = v | |
} | |
} | |
return color | |
} | |
var src = flag.String("src", "", "source image (must be PNG)") | |
var dest = flag.String("dest", "", "destination image (must be PNG)") | |
var SIZE = flag.Int("s", 10, "width / height of box") | |
func main() { | |
flag.Parse() | |
if *src == "" && *dest == "" { | |
log.Fatal("neither src nor dest can be empty") | |
} | |
reader, err := os.Open(*src) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer reader.Close() | |
writer, err := os.Create(*dest) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer writer.Close() | |
m, _, err := image.Decode(reader) | |
if err != nil { | |
log.Fatal(err) | |
} | |
bounds := m.Bounds() | |
images := make(map[color.Color]image.Image) | |
pt := image.Pt(0, 0) | |
out := image.NewRGBA(bounds) | |
for y := bounds.Min.Y; y < bounds.Max.Y; y = y + *SIZE { | |
for x := bounds.Min.X; x < bounds.Max.X; x = x + *SIZE { | |
rect := image.Rect(x, y, x+*SIZE, y+*SIZE) | |
color := dominate(m, rect) | |
if _, exists := images[color]; !exists { | |
images[color] = image.NewUniform(color) | |
} | |
pt.X = x | |
pt.Y = y | |
draw.Draw(out, rect, images[color], pt, draw.Src) | |
} | |
} | |
png.Encode(writer, out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment