Created
June 27, 2011 18:35
-
-
Save albertein/1049458 to your computer and use it in GitHub Desktop.
CodeBrawl pixelating contest entry
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') | |
width = image.width | |
height = image.height | |
size_factor = 10 | |
pixelated_width = width / size_factor | |
pixelated_height = height / size_factor | |
for x in 0.upto(pixelated_width - 1) | |
for y in 0.upto(pixelated_height - 1) #For each block on the pixelated output image | |
min_x_real = x * size_factor #we get the range of pixels from the original image | |
max_x_real = min_x_real + size_factor - 1 | |
min_y_real = y * size_factor | |
max_y_real = min_y_real + size_factor - 1 | |
count = 0 | |
total_r = 0 | |
total_g = 0 | |
total_b = 0 | |
for x_real in min_x_real..max_x_real #For each pixel on the original image corresponding | |
for y_real in min_y_real..max_y_real #to the current block of the pixelated output | |
count += 1 #we get the sum of the r, g and b components | |
color = image[x_real, y_real] | |
total_r += ChunkyPNG::Color.r color | |
total_g += ChunkyPNG::Color.g color | |
total_b += ChunkyPNG::Color.b color | |
end | |
end | |
total_r /= count #Average colors | |
total_g /= count | |
total_b /= count | |
for x_real in min_x_real..max_x_real #Paint the block color on the original image | |
for y_real in min_y_real..max_y_real | |
image[x_real, y_real] = | |
ChunkyPNG::Color.rgb(total_r, total_g, total_b) | |
end | |
end | |
end | |
end | |
image.save('output.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment