[英]Robot framework: Extent image compare function
我已经在这里提出一个问题,为RobotFramework编写了自己的图像比较功能。
from PIL import Image, ImageChops, ImageDraw, ImageFont
def check_image_files(self, file1, file2, file3) :
''' Check two image files
``file1``: absolute path to the first file
``file2``: absolute path to the second file
``file3``: absolute path to the compare file
'''
self.builtin.log("File1: %s" %file1)
self.builtin.log("File2: %s" %file2)
point_table = ([0] + ([255] * 255))
f1 = Image.open(file1)
f2 = Image.open(file2)
diff = ImageChops.difference(f1, f2)
diff = diff.convert('L')
diff = diff.point(point_table)
f3 = diff.convert('RGB')
f3.paste(f2, mask=diff)
f3.save(file3)
现在,如果文件中没有差异,则最终结果是一个完整的黑屏,但是我想返回真/假。 因此,如果2个文件不相同,我可以让测试用例通过/失败。 现在,如果文件在一小部分不相同,这不是我想要的,则测试用例成功。
我已经阅读了PIL文档,但无法获得所需的信息(顺便说一下,我是一名对编程感兴趣的测试人员)
下面的示例来自RossetaCode.org上的基本图像比较 ,它们在其中计算差异。 当然,这是确定图像是否相同的前提。 如果是,则返回0,0。
from itertools import izip
from PIL import Image
i1 = Image.open("image1.png")
i2 = Image.open("image2.png")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
print "Difference (percentage):", (dif / 255.0 * 100) / ncomponents
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.