Skip to content

Instantly share code, notes, and snippets.

@supairish
Forked from jeffkreeftmeijer/1.png
Created May 3, 2021 22:50
Show Gist options
  • Save supairish/166ddc12d9e5c6e0ba5fbd84a30d78fa to your computer and use it in GitHub Desktop.
Save supairish/166ddc12d9e5c6e0ba5fbd84a30d78fa to your computer and use it in GitHub Desktop.
Ruby image diff
require 'chunky_png'
images = [
ChunkyPNG::Image.from_file('1.png'),
ChunkyPNG::Image.from_file('2.png')
]
diff = []
images.first.height.times do |y|
images.first.row(y).each_with_index do |pixel, x|
diff << [x,y] unless pixel == images.last[x,y]
end
end
puts "pixels (total): #{images.first.pixels.length}"
puts "pixels changed: #{diff.length}"
puts "pixels changed (%): #{(diff.length.to_f / images.first.pixels.length) * 100}%"
x, y = diff.map{ |xy| xy[0] }, diff.map{ |xy| xy[1] }
images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0,255,0))
images.last.save('diff.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment