Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Created April 18, 2011 12:39
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/925244 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/925244 to your computer and use it in GitHub Desktop.
Delta-E image diff
require 'chunky_png'
include ChunkyPNG::Color
images = [
ChunkyPNG::Image.from_file('1.png'),
ChunkyPNG::Image.from_file('2.png')
]
output = ChunkyPNG::Image.new(images.first.width, images.last.width, WHITE)
diff = []
images.first.height.times do |y|
images.first.row(y).each_with_index do |pixel, x|
unless pixel == images.last[x,y]
score = Math.sqrt(
(r(images.last[x,y]) - r(pixel)) ** 2 +
(g(images.last[x,y]) - g(pixel)) ** 2 +
(b(images.last[x,y]) - b(pixel)) ** 2
) / Math.sqrt(MAX ** 2 * 3)
output[x,y] = grayscale(MAX - (score * 255).round)
diff << score
end
end
end
puts "pixels (total): #{images.first.pixels.length}"
puts "pixels changed: #{diff.length}"
puts "image changed (%): #{(diff.inject {|sum, value| sum + value} / images.first.pixels.length) * 100}%"
output.save('diff.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment