ImageMagick 提供compare
功能来正确比较图像。
检查md5
两个图像的校验和不是正确的方法,因为某些图像格式(例如带有 EXIF 的 PNG 和 JPEG)包含创建文件的日期和时间(请参见下面的示例 1),并且某些文件可能在视觉上相同但表示形式相同内部完全不同(参见示例 2),或者具有不同的位深度(参见示例 3)。
实施例1
# Create two identical images (100x100 pixels, bright red) but with different file contents
convert -size 100x100 xc:red r1.png
convert -size 100x100 xc:red r2.png
# MD5 checksum them
md5 r?.png
MD5 (r1.png) = 9f6d612615efd88c3fd8521d717e9811
MD5 (r2.png) = 996911bec0e0da75af46a1e78c052998 # Mmmm different
# Ask IM to tell us absolute error between the two (number of differing pixels)
compare -metric AE r1.png r2.png null:
0 # No difference - that's better
为什么这两者的 MD5 不同?因为日期就在里面...
identify -verbose r[12].png | grep -i date
date:create: 2015-03-03T14:57:26+00:00
date:modify: 2015-03-03T14:57:26+00:00
date:create: 2015-03-03T14:57:43+00:00
date:modify: 2015-03-03T14:57:43+00:00
实施例2
# Create PNG and identical GIF
convert -size 100x100 xc:red r.png
convert -size 100x100 xc:red r.gif
# Compare with MD5 sums
md5 r.png r.gif
MD5 (r.png) = 692ef06b62a15b799d5dc549b0dd3737
MD5 (r.gif) = 549feea78dc438924fbb3e0ef97dc0b3 # Ooops
# Compare properly
compare -metric AE r.gif r.png null:
0 # Identical
实施例3
# Create 8-bit PNG and 16-bit PNG
convert -size 100x100 xc:red PNG8:8.png
convert -size 100x100 xc:red PNG48:48.png
# MD5 sum them
md5 8.png 48.png
MD5 (8.png) = eb3fc9a06e1632c3b41ebb986b81a816
MD5 (48.png) = 32fdf1c30793a4fed941c91d27084e0a # Ooops
# Let ImageMagick compare them
compare -metric AE 8.png 48.png null:
0
图像模糊比较
正如库尔特提到的,这也导致了做一个fuzzy图像比较。我们可以这样探索:
# Create a grey image, 100x100 and put some noise in it
convert -size 100x100 xc:gray +noise gaussian noise.png
现在将所有像素乘以 1.01,使它们的亮度提高难以察觉的 1%:
# Make pixels 1% brighter
convert noise.png -evaluate multiply 1.01 brighternoise.png
# ... and compare the statistics of the two images
identify -verbose *noise* | grep -E "^Image|mean"
Image: brighternoise.png
mean: 127.235 (0.498959) <--- The brighter image is, well, brighter
Image: noise.png
mean: 126.175 (0.494805)
现在通过几种不同的方式对它们进行比较:
# Pixels may differ by up to 2% before being considered different
compare -fuzz 2% -metric AE noise.png brighternoise.png null:
0 # All pixel values within 2% between the 2 images
# Pixels may only differ by 0.5% before being considered different
compare -fuzz 0.5% -metric AE noise.png brighternoise.png null:
594 # 594 of the 10,000 pixels differ by more than 0.5%
# Calculate Root Mean Square Error (RMSE) to see how much pixels tend to differ
compare -metric RMSE noise.png brighternoise.png null:
278.96 (0.00425666) # On average, the pixels differ by 0.4% - i.e. hardly at all