如何将本地目录中的图像与下载的图像进行比较。比较应基于图像内容和大小。
如何在ruby中执行此操作?
答案 0 :(得分:0)
有几种方法可以做到这一点。
1)我认为最好的方法是使用Rmagick签名(感谢this useful manual):
require 'RMagick'
new_image = Magick::Image.read(new_photo)[0] ## new_photo = "/your/dir/file.jpg"
expected_image = Magick::Image.read(expected_photo)[0]
new_image.signature.should eql expected_image.signature
或
diff_img, diff_metric = img1[0].compare_channel( img2[0], Magick::MeanSquaredErrorMetric )
2)您也可以使用raster_graphics库(rosettacode.org):
require 'raster_graphics'
class RGBColour
# the difference between two colours
def -(a_colour)
(@red - a_colour.red).abs +
(@green - a_colour.green).abs +
(@blue - a_colour.blue).abs
end
end
class Pixmap
# the difference between two images
def -(a_pixmap)
if @width != a_pixmap.width or @height != a_pixmap.height
raise ArgumentError, "can't compare images with different sizes"
end
sum = 0
each_pixel {|x,y| sum += self[x,y] - a_pixmap[x,y]}
Float(sum) / (@width * @height * 255 * 3)
end
end
lenna50 = Pixmap.open_from_jpeg('Lenna50.jpg')
lenna100 = Pixmap.open_from_jpeg('Lenna100.jpg')
puts "difference: %.5f%%" % (100.0 * (lenna50 - lenna100))
#=>:
#difference: 1.62559%