from PIL import Image
import os
def RGB_compare(base_layer_path, min_size_path, max_size_path, save_path):
# 加载图像min表示图像1,max表示图像2
min_size = Image.open(min_size_path)
min_size = min_size.convert('RGBA')
max_size = Image.open(max_size_path)
max_size = max_size.convert('RGBA')
base_layer = Image.open(base_layer_path)
base_layer = base_layer.convert('RGBA')
both = 0 # 记录两幅图都预测出的像素个数
max_min = 0 # 记录max预测出但是min没有预测出的像素个数
min_max = 0 # 记录min预测出但是max没有预测出的像素个数
# 获取图像尺寸
heigh, width = min_size.size
for i in range(heigh):
for j in range(width):
# 以max作为图像判别标准
min_r, min_g, min_b, min_a = min_size.getpixel((i, j))
max_r, max_g, max_b, max_a = max_size.getpixel((i, j))
# 如果max预测出来了
if max_r == 128:
# 如果两张图像都预测出来了便标记为红色
if min_r == 128:
both += 1
min_size.putpixel((i, j), (255, 0, 0, 255))
# 大有小没有便标记为白色
if min_r == 0:
max_min += 1
min_size.putpixel((i, j), (255, 255, 255, 255))
elif max_r == 0:
# 如果两个都没预测出来,直接设置为背景
if min_r == 0:
min_size.putpixel((i, j), (0, 0, 0, 0))
# 如果小的有大的没有,设置为绿色
if min_r == 128:
min_max += 1
min_size.putpixel((i, j), (0, 255, 0, 255))
print('both:{},max_min:{},min_max:{}'.format(both, max_min, min_max))
base_layer.paste(min_size, (0, 0), min_size) # 贴图操作
base_layer.save(save_path + "/" + "1024_1280.png") # 图片保存
if __name__ == "__main__":
base_layer_path = r''
min_size_path = r''
max_size_path = r''
save_path = r''
RGB_compare(base_layer_path, min_size_path, max_size_path, save_path)
python实现图像差异性分析(标记并记录差异点)
最新推荐文章于 2023-05-17 10:09:13 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)