Skip to content

Instantly share code, notes, and snippets.

@will
Created June 28, 2011 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save will/1050731 to your computer and use it in GitHub Desktop.
Save will/1050731 to your computer and use it in GitHub Desktop.
codebrawl2
require 'chunky_png'
include ChunkyPNG
# ten by ten pixles?
# might as well go 8 bit
# while we are at it
def reduce_depth(color, bits)
Color.rgb(*Color.to_truecolor_bytes(color).
map{ |c| c.to_f/256 }.each_with_index.map { |c,i|
(256.0 * (c*bits[i]).round / bits[i]).to_i
})
end
def reduce_image(output, bits)
image = Image.from_file('input.png')
(0..image.width-1).step(10).each do |x|
(0..image.height-1).step(10).each do |y|
color = reduce_depth image[x,y], bits
10.times {|i| 10.times {|j|
image.set_pixel_if_within_bounds x+i, y+j, color
}}
end
end
image.save("#{output}.png")
end
reduce_image "8bit", [8,8,4]
reduce_image "4bit_r", [4,2,2]
reduce_image "4bit_g", [2,4,2]
reduce_image "4bit_b", [2,2,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment