一:问题描述:使用ImageChops.difference(image_one, image_two).save(location)后发现保存的图片是全黑色。
二:代码如下:
from PIL import Image
from PIL import ImageChops
standard_path = r'E:\report\AI2_reverse\\74_1.png'
cut_path = r'E:\report\AI2_reverse\2021_10_29_09_19_18\test_first\\74_1.png'
different_path = r'E:\report\AI2_reverse\2021_10_29_09_19_18\different\\74_1_test.png'
def compare_images(path_one, path_two, diff_save_location):
image_one = Image.open(path_one)
image_two = Image.open(path_two)
#比较标准图片和截图,然后保存对比结果图
diff = ImageChops.difference(image_one, image_two)
diff.save(diff_save_location)
if __name__=='__main__':
compare_images(standard_path, cut_path, different_path) #对比图片,并保存对比后的图片
三:结果
代码运行没有报错,但是查看保存后的图片后发现是一张全部黑色的图,期望图片应该是会显示两张图片不同区域。
四:思路
同事运行这段代码时保存后的图片是显示了不同区域内容,区别是
同事python:3.9版本,PIL:8.1.1版本
我的python:3.10版本,PIL:8.4.0版本
那就换成版本一致阿。
可能是图片格式有要求,在这两句代码后面加上转换图片格式后解决
image_one = Image.open(path_one).convert("RGB")
image_two = Image.open(path_two).convert("RGB")