Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created July 2, 2011 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/1059942 to your computer and use it in GitHub Desktop.
Save jamesu/1059942 to your computer and use it in GitHub Desktop.
# Code for http://codebrawl.com/contests/pixelizing-images-with-chunkypng - @jamesu
require 'rubygems'
require 'open-uri'
require 'chunky_png'
open("https://gist.github.com/raw/5dace61b37de19a56637/032f70a3023e37a7dc1fe4a619cd8c4f970d1677/output.png") do |f|
p=ChunkyPNG;image=p::Image.from_blob(f.read);col=p::Color
(0...(image.width)).each_slice(10) {|x|
(0...(image.height)).each_slice(10) {|y|
sum = [0,0,0]
x.each{|xp| y.each{|yp|
p = image.get_pixel(xp,yp)
sum[0]+=col.r(p); sum[1]+=col.g(p); sum[2]+=col.b(p)
}}
sum.map!{|c| c /= x.length*y.length}
sum_color = ChunkyPNG::Color.rgb(sum[0],sum[1],sum[2])
image.rect(x[0],y[0],x[0]+9,y[0]+9,sum_color,sum_color)
}}
image.save('output.png')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment