Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Created May 2, 2011 12:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/951528 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/951528 to your computer and use it in GitHub Desktop.
ChunkyPNG nearest-neighbor resize
require 'chunky_png'
image = ChunkyPNG::Image.from_file('bassie.png')
[2,0.5].each do |scale|
resized_image = ChunkyPNG::Image.new((image.width * scale).round, (image.height * scale).round)
resized_image.pixels.map!.with_index do |pixel, index|
x, y = index % resized_image.width, (index / resized_image.width).floor
image[x / scale, y / scale]
end
resized_image.save("bassie_resized_#{scale}.png")
end
@jeffkreeftmeijer
Copy link
Author

Please let me know if you know any way of improving this, was having some trouble converting the pixels' indexes to x/y coordinates. :)

@wvanbergen
Copy link

I already implemented nearest neighbor resampling in ChunkyPNG: https://github.com/wvanbergen/chunky_png/blob/master/lib/chunky_png/canvas/resampling.rb

My code is significantly more complex then yours though, so patch so simplify it would be appreciated. Are you tackling bicubic resampling next? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment