Skip to content

Instantly share code, notes, and snippets.

@mikewoodhouse
Created July 1, 2011 08:56
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 mikewoodhouse/1058123 to your computer and use it in GitHub Desktop.
Save mikewoodhouse/1058123 to your computer and use it in GitHub Desktop.
require 'chunky_png'
require 'backports' unless RUBY_VERSION >= "1.8.7"
include ChunkyPNG
image = Image.from_file('input.png')
xslices = (0...image.width).each_slice(10).to_a
yslices = (0...image.height).each_slice(10).to_a
xslices.product(yslices) do |xslice, yslice|
tots = [0, 0, 0, 0]
xslice.product(yslice).each do |x, y|
rgba = Color.to_truecolor_alpha_bytes(image[x,y])
tots.map! { |t| t + rgba.shift }
end
weight = xslice.size * yslice.size
pixelzd_color = Color.rgba(*tots.map { |t| (t.to_f / weight).round })
image.rect(xslice.first, yslice.first, xslice.last, yslice.last, Color::TRANSPARENT, pixelzd_color)
end
image.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment