Search code examples
pythonpython-3.xpython-imaging-library

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.

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.

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?


Solution

  • I mean PIL works with numpy arrays or at least can be easily converted to them, so I'd just work with that.

    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.