Created
June 27, 2011 19:08
-
-
Save wilkerlucio/1049567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
