【问题标题】:Robot framework: Extent image compare function机器人框架:范围图像比较功能
【发布时间】:2017-08-29 04:49:52
【问题描述】:

借助此处提出的问题,我为 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)

如果在文件中没有发现任何差异,现在的最终结果是完全黑屏,但我想得到一个真/假。所以如果两个文件不相同,我可以让测试用例通过/失败。现在,如果文件的一小部分不相同,那么测试用例就会成功,这不是我想要的。

我已阅读 PIL 文档,但无法获得所需的内容(顺便说一句,我是一名对编程感兴趣的测试人员)

【问题讨论】:

标签: python robotframework


【解决方案1】:

下面的示例来自 basic image comparison 上的 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

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 2012-01-27
    • 2020-07-19
    • 2018-02-12
    • 2016-08-23
    • 2018-03-28
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多