Created
July 2, 2011 21:43
-
-
Save kourge/1061682 to your computer and use it in GitHub Desktop.
Pixelizing images with ChunkyPNG - Codebrawl
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
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