我正在使用PIL比较2张图片/图片。下面的代码适用于某些图片但不是全部。
from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw
im1 = Image.open(r'c:\a.jpg')
im2 = Image.open(r'c:\aa.jpg')
diff = ImageChops.difference(im2, im1).getbbox()
print diff
draw = ImageDraw.Draw(im2)
draw.rectangle(diff)
im2 = im2.convert('RGB')
im2.save(r'c:\aaa.jpg')
例如,它不适用于这两张照片。
A.JPG
aa.jpg
输出为(16,80,80,144),但它不会在图片上绘制任何内容。
问题:
感谢。
答案 0 :(得分:2)
from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw
im1 = Image.open('a.jpg')
im2 = Image.open('aa.jpg')
diff = ImageChops.difference(im2, im1).getbbox()
print diff
draw = ImageDraw.Draw(im2)
draw.rectangle(diff, outline = (0,255,0))
print help(draw.rectangle)
im2 = im2.convert('RGB')
im2.save('aaa.jpg')
模块PIL.ImageDraw中方法矩形的帮助:
矩形(self,xy,fill = None,outline = None)方法 PIL.ImageDraw.ImageDraw实例
所以大纲参数默认为无,这就是为什么它在这里创建透明矩形。