1

I have no background in image-processing. I am interested in getting the difference between these two images. enter image description here

enter image description here

After writing the following code :

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")

I get this output :-

enter image description here

I am looking for some customisations here :

1) I want to label the differences in output in different colours. Things from the 1.png and 2.png should have a different colours.

2) background should be white.

3) I want my output to have axises and axis labels. Would it be possible somehow ?

Grayrigel
  • 3,474
  • 5
  • 14
  • 32

1 Answers1

2

You probably can't do this with the high-level difference method, but it's quite easy if you compare the images pixel by pixel yourself. Quick attempt:

enter image description here

Code:

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")

This assumes that the images are the same size and have identical axes.

Grayrigel
  • 3,474
  • 5
  • 14
  • 32
C. Ramseyer
  • 2,322
  • 2
  • 18
  • 22