Skip to content

Instantly share code, notes, and snippets.

@kourge
Created July 2, 2011 21:43
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 kourge/1061682 to your computer and use it in GitHub Desktop.
Save kourge/1061682 to your computer and use it in GitHub Desktop.
Pixelizing images with ChunkyPNG - Codebrawl
require 'chunky_png'
image = ChunkyPNG::Image.from_file('input.png')
BLOCK_SIZE = 10
(0...image.height).each_slice(BLOCK_SIZE) do |y_range|
(0...image.width).each_slice(BLOCK_SIZE) do |x_range|
block = x_range.map { |x| y_range.map { |y| [x, y] } }.flatten(1)
r, g, b, a, size = 0, 0, 0, 0, block.size
block.each do |x, y|
m, n, o, p = ChunkyPNG::Color.to_truecolor_alpha_bytes(image[x, y])
r, g, b, a = r + m, g + n, b + o, a + p
end
avg = ChunkyPNG::Color.rgba(r / size, g / size, b / size, a / size)
block.each { |x, y| image[x, y] = avg }
end
end
image.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment