-
-
Save awesome/7bb889332c913f3fbe26bcf8ad49b7e4 to your computer and use it in GitHub Desktop.
Pixelizing images with ChunkyPNG
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
| #! usr/bin/env ruby | |
| require 'chunky_png' | |
| class ChunkyPNG::Image | |
| # s: Integer (pixel size) | |
| def pixelize s = 10 | |
| temp = Array.new((height*1.0/s).ceil) {Array.new((width*1.0/s).ceil) {Array.new(3) {0}}} | |
| height.times {|j| width.times {|i| ChunkyPNG::Color.to_truecolor_bytes(get_pixel(i,j)).each.with_index {|e,k| temp[j/s][i/s][k] += e}}} | |
| png = ChunkyPNG::Image.new width, height | |
| sq = s**2 | |
| height.times {|j| width.times {|i| png.set_pixel(i,j, ChunkyPNG::Color.parse(ChunkyPNG::Color.rgb(*temp[j/s][i/s].map {|e| e/sq})))}} | |
| png | |
| end | |
| end | |
| image = ChunkyPNG::Image.from_file('input.png') | |
| image.pixelize.save('output_average.png') |
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
| #! usr/bin/env ruby | |
| require 'chunky_png' | |
| # ChunkyPNG::Image#resize is already implemented | |
| image = ChunkyPNG::Image.from_file('input.png') | |
| w, h = image.width, image.height | |
| image.resize(w/10, h/10).resize(w, h).save('output_cheat.png') |
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
| #! usr/bin/env ruby | |
| require 'chunky_png' | |
| class ChunkyPNG::Image | |
| # s: Integer (pixel size) | |
| def pixelize s = 10 | |
| png = ChunkyPNG::Image.new width, height | |
| height.times {|j| width.times {|i| png.set_pixel(i,j, get_pixel(i/s*s, j/s*s))}} | |
| png | |
| end | |
| end | |
| image = ChunkyPNG::Image.from_file('input.png') | |
| image.pixelize.save('output_nearest.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


