Skip to content

Instantly share code, notes, and snippets.

@xenocid
Created June 28, 2011 18:19
Show Gist options
  • Select an option

  • Save xenocid/1051795 to your computer and use it in GitHub Desktop.

Select an option

Save xenocid/1051795 to your computer and use it in GitHub Desktop.
My entry to the 2nd round of the CodeBrawl
require 'chunky_png'
include ChunkyPNG::Color
include ChunkyPNG::Canvas::Drawing
input = ChunkyPNG::Image.from_file('input.png')
# Put your logic to averageize `image` here.
BLOCK_SIZE = 10
height = input.height
width = input.width
heightIterations = height / BLOCK_SIZE
widthIterations = width / BLOCK_SIZE
canvas = ChunkyPNG::Canvas.new(width, height, WHITE)
def averageArray(array)
return (array.inject { |sum, el| sum + el }.to_f / array.size).to_i
end
heightIterations.times do |y|
widthIterations.times do |x|
reds, greens, blues = [], [], []
BLOCK_SIZE.times do |cy|
BLOCK_SIZE.times do |cx|
pixel = input[x * BLOCK_SIZE + cx, y * BLOCK_SIZE + cy]
index = cy * BLOCK_SIZE + cx
reds[index], greens[index], blues [index] = r(pixel), g(pixel), b(pixel)
end
end
average = rgb(averageArray(reds), averageArray(greens), averageArray(blues))
canvas.rect(x * BLOCK_SIZE, y * BLOCK_SIZE, (x+1) * BLOCK_SIZE, (y+1) * BLOCK_SIZE, average, average)
end
end
# Save image
canvas.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment