Skip to content

Instantly share code, notes, and snippets.

@wilkerlucio
Created June 27, 2011 19:08
Show Gist options
  • Select an option

  • Save wilkerlucio/1049567 to your computer and use it in GitHub Desktop.

Select an option

Save wilkerlucio/1049567 to your computer and use it in GitHub Desktop.
require 'chunky_png'
module ChunkyPNG
class Canvas
module Pixelate
# Pixelate image or part of the image
# @param [Integer] block size for pixalating
# @param [Integer] start x-coordinate for pixelating
# @param [Integer] start y-coordinate for pixelating
# @param [Integer] end x-coordinate for pixelating
# @param [Integer] end y-coordinate for pixelating
# @return [ChunkyPNG::Canvas] Itself, with pixelated applied
def pixelate(block_size, x0 = 0, y0 = 0, x1 = self.width - 1, y1 = self.height - 1)
half_block = block_size / 2
x = x0
y = y0
while y <= y1
while x <= x1
x_color_point = min(x + half_block, width - 1)
y_color_point = min(y + half_block, height - 1)
x_end = min(x + block_size - 1, x1)
y_end = min(y + block_size - 1, y1)
color = self.get_pixel(x_color_point, y_color_point)
self.rect(x, y, x_end, y_end, ChunkyPNG::Color::TRANSPARENT, color)
x += block_size
end
x = x0
y += block_size
end
self
end
private
# internal simple function to calculate minimum of two values
def min(a, b)
return a if a < b
return b
end
end
# include module on Canvas
include Pixelate
end
end
image = ChunkyPNG::Image.from_file('input.png')
image.pixelate(10)
image.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment