gpt4 book ai didi

python - 如何自定义图像差异生成的输出?

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:00 24 4
gpt4 key购买 nike

我没有图像处理方面的背景。我有兴趣了解这两个图像之间的差异。 enter image description here

enter image description here

编写以下代码后:

from PIL import Image
from PIL import ImageChops

im1 = Image.open("1.png")
im2 = Image.open("2.png")

diff = ImageChops.difference(im2, im1)
diff.save("diff.png")

我得到这个输出:-

enter image description here

我正在寻找一些定制:

1)我想用不同的颜色标记输出的差异。 1.png 和 2.png 中的内容应该具有不同的颜色。

2) 背景应该是白色的。

3)我希望我的输出具有轴和轴标签。有可能以某种方式实现吗?

最佳答案

您可能无法使用高级差分方法来做到这一点,但如果您自己逐像素比较图像,这将非常容易。快速尝试:

enter image description here

代码:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

im1 = Image.open("im1.jpeg").convert('1') # binary image for pixel evaluation
rgb1 = Image.open("im1.jpeg").convert('RGB') # RGB image for border copy
p1 = im1.load()
prgb1 = rgb1.load()

im2 = Image.open("im2.jpeg").convert('1') # binary image for pixel evaluation
p2 = im2.load()

width = im1.size[0]
height = im1.size[1]

imd = Image.new("RGB", im1.size)
draw = ImageDraw.Draw(imd)
dest = imd.load()
fnt = ImageFont.truetype('/System/Library/Fonts/OpenSans-Regular.ttf', 20)

for i in range(0, width):
for j in range(0, height):

# border region: just copy pixels from RGB image 1
if j < 30 or j > 538 or i < 170 or i > 650:
dest[i,j] = prgb1[i,j]
# pixel is only set in im1, make red
elif p1[i,j] == 255 and p2[i,j] == 0:
dest[i,j] = (255,0,0)
# pixel is only set in im2, make blue
elif p1[i,j] == 0 and p2[i,j] == 255:
dest[i,j] = (0,0,255)
# unchanged pixel/background: make white
else:
dest[i,j] = (255,255,255)


draw.text((700, 50),"blue", "blue", font=fnt)
draw.text((700, 20),"red", "red", font=fnt)
imd.show()
imd.save("diff.png")

这假设图像大小相同并且具有相同的轴。

关于python - 如何自定义图像差异生成的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42041554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com