ImageMagickに同梱されているcompare
コマンドが簡単で便利。
compare -metric PSNR ref.jpg target.jpg diffpng
2つのフォルダに入っている画像を連続で比較するrubyスクリプトは下記の通り。
compareの結果はなぜか標準エラー出力に出てくるので注意。
# !/bin/env ruby
# -*- coding: utf-8 -*-
#
# Usage
# $ ruby comp.rb TARGET_FOLDER
#
require 'rubygems'
require 'systemu'
# http://www.imagemagick.org/script/command-line-options.php#metric
COMPARE_METRIC = "PSNR"
REF_FOLDER = "RefFolder"
REF_FORMAT_EXT = ".bmp"
TARGET_FOLDER = ARGV[0]
Dir.glob(File.join(TARGET_FOLDER, "*")).sort.each do |target_file|
ref_file = File.join(REF_FOLDER, File.basename(target_file, ".jpg")) + REF_FORMAT_EXT
command = "compare -metric #{COMPARE_METRIC} #{ref_file} #{target_file} diff.png"
# Metrics value is outputed to stderr somehow
status, stdout, stderr = systemu command
puts "#{target_file}\t#{stderr}"
end