I am comparing 2 images/pictures by using PIL. Below codes work on some pictures but not all.
from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw
im1 = Image.open(r'c:\a.jpg')
im2 = Image.open(r'c:\aa.jpg')
diff = ImageChops.difference(im2, im1).getbbox()
print diff
draw = ImageDraw.Draw(im2)
draw.rectangle(diff)
im2 = im2.convert('RGB')
im2.save(r'c:\aaa.jpg')
For example, it doesn’t work for these 2 pictures.
a.jpg
aa.jpg
the output is (16, 80, 80, 144) however it doesn't draw anything on the picture.
Questions:
- Why is it happening?
- Does the file type matter? i.e. JPG compares with JPG; PNG compares with PNG; BMP compares with BMP – which format is best for comparison?
- Sometimes the difference lies on far distance on the picture, so it will draw a big rectangle to include the whole area. Is there a way to only draw small rectangle to mark the differeces?
Thanks.