与PIL的图像比较未按预期工作

Rag*_*gav 0 python image-processing python-imaging-library

我正在尝试与linux x屏幕截图图像进行图像比较

我遵循python脚本

http://aatiis.me/2010/08/12/fast-image-comparison-with-python.html

它可以工作并为样本鸭图像提供输出(snap_from_x_duck在此输入图像描述 AND resized_from_duck在此输入图像描述)我用于测试,但当我加载我的实际图像(snap_from_x.png在此输入图像描述 AND resized_from_org.png在此输入图像描述 )它给出了一个错误..

  Comparing 1 images:
 *  1 /  1: /opt/ad_re.png /opt/op.png ...
 Traceback (most recent call last):
     File "imgcmp.py", line 246, in <module>
         sim = cmp.similarity()
     File "imgcmp.py", line 212, in similarity
         cmp = self.compare()
     File "imgcmp.py", line 180, in compare
         diff.append(cmp.levenshtein)
     File "imgcmp.py", line 127, in levenshtein
         stra_r = ''.join((chr(x>>16) for x in self.imga_int))
     File "imgcmp.py", line 50, in imga_int
         self._imga_int = tuple(self._img_int(self._imga))
     File "imgcmp.py", line 120, in _img_int
         yield pixel[0] | (pixel[1]<<8) | (pixel[2]<<16)
 TypeError: 'int' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)

它很困惑,完全需要这方面的帮助

jsb*_*eno 5

什么是(非常)可能发生的是,配方只能用于RGB图像 - 而你的图像是一个调色版本,每个像素只有1个字节(因此,图像的像素是单个字节,而不是具有RGB值的可迭代).

解决方法是在调用比较函数之前将图像转换为RGB - 只需:

img = img.convert("RGB")