Skip to content

Instantly share code, notes, and snippets.

@hng
Created June 30, 2011 16:37
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 hng/1056618 to your computer and use it in GitHub Desktop.
Save hng/1056618 to your computer and use it in GitHub Desktop.
Solution Codebrawl #2
require 'chunky_png'
@image = ChunkyPNG::Image.from_file('input.png')
def pixelate(a, b)
if a+10 > @image.dimension.width
w = @image.dimension.width - a
else
w = 10
end
if b+10 > @image.dimension.height
h = @image.dimension.height - b
else
h = 10
end
color1 = @image[a, b]
w.times do |i|
h.times do |j|
color2 = @image[i+a, j+b]
c1_r = ChunkyPNG::Color.r(color1)
c2_r = ChunkyPNG::Color.r(color2)
c1_g = ChunkyPNG::Color.g(color1)
c2_g = ChunkyPNG::Color.g(color2)
c1_b = ChunkyPNG::Color.b(color1)
c2_b = ChunkyPNG::Color.b(color2)
color1 = ChunkyPNG::Color.rgb(((c1_r + c2_r)/2), ((c1_g + c2_g)/2), ((c1_b + c2_b)/2))
end
end
w.times do |i|
h.times do |j|
@image[i+a, j+b] = color1
end
end
end
w = dim.width
h = dim.height
i = 0
while (i < w)
j = 0
while (j < h)
pixelate(i, j)
j = j + 10
end
i = i + 10
end
@image.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment