简体   繁体   English

根据使用 PIL 的比较结果更改像素

[英]Changing a pixel based on the results of a comparison using PIL

I am trying to compare two images that appear to be the same but the RGB average differs for each.我正在尝试比较两个看起来相同但 RGB 平均值不同的图像。

I have created a new blank image and I will plot a coloured pixel if the two images match in that location and if they don't match then it will plot a different colour pixel hopefully highlighting the pixel locations of the altered pixels.我已经创建了一个新的空白图像,如果两个图像在该位置匹配,我将 plot 一个彩色像素,如果它们不匹配,那么它将 plot 一个不同的颜色像素,希望突出显示更改像素的像素位置。

diff_img = Image.new('RGB', (width,height), "black")
map_diff = diff_img.load()
Good_count = 0

for i in range(open_img1.size[0]):
    for j in range(open_img1.size[1]):
        if File1[i] == File2[i] and File2[j] == File2[j]:
            Good_count += 1
        else:
            map_diff[i,j] = (255, 255, 255) # set the colour accordingly

diff_img.show()

This currently gives me just a black image so no pixels have been altered on the new image I'm not sure where I'm going wrong as from what I can see if the row and column of the two files don't match it will change the pixel to a white one?这目前只给我一个黑色图像,所以新图像上没有像素被改变我不确定我哪里出错了,因为如果两个文件的行和列不匹配,我可以看到将像素更改为白色?

I mean PIL works with numpy arrays or at least can be easily converted to them, so I'd just work with that.我的意思是 PIL 可以与 numpy arrays 一起使用,或者至少可以轻松转换为它们,所以我只使用它。

from PIL import Image    
img = np.array(Image.open(File1)) 
img2 = np.array(Image.open(File2))
img3 = img == img2
Image.fromarray(img3).save("test.png")  

This should already do it.这应该已经做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM