Skip to content

Instantly share code, notes, and snippets.

@dyerw
Last active August 29, 2015 14:16
Show Gist options
  • Save dyerw/85ed8b18a2bed6eaff7e to your computer and use it in GitHub Desktop.
Save dyerw/85ed8b18a2bed6eaff7e to your computer and use it in GitHub Desktop.
Takes the difference of two png files
require 'chunky_png'
include ChunkyPNG::Color
images = [ChunkyPNG::Image.from_file('dog1.png'), # Put yr files here
ChunkyPNG::Image.from_file('dog2.png')]
output = ChunkyPNG::Image.new(images.first.width, images.last.height, rgb(255, 255, 255))
height = images.first.height > images.last.height ? images.last.height : images.first.height
width = images.first.width > images.last.width ? images.last.width : images.first.width
total_pixels = height * width
output = ChunkyPNG::Image.new(width, height, rgb(255, 255, 255))
puts "Analyzing diff for #{height} x #{width} result image with #{total_pixels} total pixels"
different_pixels = 0
height.times do |y|
width.times do |x|
rdiff = (r(images.first[x, y]) - r(images.last[x, y])).abs
gdiff = (g(images.first[x, y]) - g(images.last[x, y])).abs
bdiff = (b(images.first[x, y]) - b(images.last[x, y])).abs
if rdiff + gdiff + bdiff > 0
different_pixels += 1
end
output.compose_pixel(x, y, rgb(rdiff, gdiff, bdiff))
end
end
puts "#{different_pixels} different pixels"
puts "#{ (different_pixels.to_f / total_pixels.to_f) * 100 }% difference"
output.save("diff.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment