Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jmorton
Created June 27, 2011 17:08
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 jmorton/1e7291f5337fc18be247 to your computer and use it in GitHub Desktop.
Save jmorton/1e7291f5337fc18be247 to your computer and use it in GitHub Desktop.
Chunky
require 'chunky_png'
module Pixelize
# Replace chunks of an image with a single color.
#
# @param size Number of pixels to consolidate
#
def pixelize!(size)
# Step over each chunk horizontally and vertically, being careful
# not to exceed the boundaries (hence subtracting one).
0.step(self.width-1, size) do |x1|
x2 = x1+size-1
0.step(self.height-1, size) do |y1|
y2 = y1+size-1
rect(x1, y1, x2, y2, ChunkyPNG::Color::TRANSPARENT, self[x1,y1])
end
end
end
end
class ChunkyPNG::Image
include Pixelize
end
img = ChunkyPNG::Image.from_file('output.png')
img.pixelize!(10)
img.save('chunky3.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment