Skip to content

Instantly share code, notes, and snippets.

@awesome
Forked from darkleo/chunkypixel_average.rb
Created March 23, 2017 10:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save awesome/7bb889332c913f3fbe26bcf8ad49b7e4 to your computer and use it in GitHub Desktop.
Pixelizing images with ChunkyPNG
#! 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')
#! 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')
#! 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