Skip to content

Instantly share code, notes, and snippets.

@mfo
Created April 25, 2012 22:37
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 mfo/2494040 to your computer and use it in GitHub Desktop.
Save mfo/2494040 to your computer and use it in GitHub Desktop.
simple command line utility to create diff image of two URL.
#!/usr/bin/env ruby
# usage in the real life:
# when you want to compare changes between your development version and a production version :
# webdiff http://sharypic.com http://sharypic-dev.com:3000
#
# into the wild:
# create a 'test suite' of your full website :-) [i'll parse my sitemap.xml]
require 'fileutils'
require 'chunky_png'
puts "usage: webdiff production_website dev_website" and return 0 if ARGV.size != 2
# desc: get two url then snapshot them in order to return a path to a diff image
# url_to_snapshot_a: valid URL to website_a
# url_to_snapshot_b: valid URL to website_a'
# return a path to a diff image of website_a/website_a'
def process(url_to_snapshot_a, url_to_snapshot_b)
path_to_snapshots = [url_to_snapshot_a, url_to_snapshot_b].map{ |url| snapshot url }
create_image_diff(*path_to_snapshots)
end
def show_diff_image(path_to_image)
%x[open #{path_to_image}]
end
# desc: take snapshot of the given url and return the path of the generated .PNG file
# under the hood, using webkit2png. see: http://www.paulhammond.org/webkit2png
# aliased: ln -s your_web2png.py /usr/bin/web2png
#
# url: accessible URL
# return: path to snapshoted URL png file
def snapshot url
basename = url.gsub /[^a-zA-Z0-9]/, ""
webkit_to_png_convention = "-full.png"
%x[webkit2png -o #{basename} #{url}]
"#{basename}#{webkit_to_png_convention}"
end
# desc: compare differences between two images,
# path_to_img_a: path to reference image
# path_to_img_b: path to potentialy updated image
# return: path to a newly created file highlighting area difference
# credits: go to http://jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs
def create_image_diff(path_to_img_a, path_to_img_b)
images = [path_to_img_a, path_to_img_b].map{ |path_to_img| ChunkyPNG::Image.from_file(path_to_img) }
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
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')
'diff.png'
end
show_diff_image process(*ARGV)
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment