Skip to content

Instantly share code, notes, and snippets.

@elektronaut
Created June 27, 2011 20:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elektronaut/71598aeb1d823c9229ac to your computer and use it in GitHub Desktop.
Save elektronaut/71598aeb1d823c9229ac to your computer and use it in GitHub Desktop.
Codebrawl #2 entry
require 'chunky_png'
module ChunkyPNG
class Image
def pixelize!(size=10)
[[:row, height], [:column, width]].each do |orientation, length|
for i in 0...length
pixelated = []; send(orientation, i).each_slice(size) do |slice|
pixelated += [Color.rgba(*[:r,:g,:b,:a].map{|chan|
(slice.map{|c| Color.send(chan, c)}.inject(0.0){|sum, v| sum += v} / slice.length).round
})] * slice.length
end
send("replace_#{orientation}!", i, pixelated)
end
end
end
end
end
image = ChunkyPNG::Image.from_file('input.png')
image.pixelize!
image.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment