Mark K
Mark K

Reputation: 9348

Using Image (PIL) to save the file marked a difference between 2 pictures

I am comparing 2 pictures and want to mark the difference and save it in a new file.

(Python 2.7 + Windows)

What I am doing is below:

from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw

file1 = 'Animal Nov 2014.jpg'
file2 = ' Animal May 2014.jpg'

im1 = Image.open(file1)
im2 = Image.open(file2)

diff = ImageChops.difference(im1, im2).getbbox()
draw = ImageDraw.Draw(im2)
draw.rectangle(diff)
im2.save('file3.jpg')

when I save it to 'file3.jpg', it gives error:

IOError: cannot write mode P as JPEG

when I save it to 'file3.png', it gives error:

TypeError: an integer is required

How can I have it saved to a new file? Thanks.



please see the solution at PIL (Image) ValueError: Not a valid number of quantization tables. Should be between 1 and 4

Upvotes: 0

Views: 1556

Answers (1)

physicalattraction
physicalattraction

Reputation: 6858

The answer can be found in this thread: Getting "cannot write mode P as JPEG" while operating on JPG image

You need to convert the image to RGB mode.

In your case:

im2.convert('RGB').save('file3.jpg')

Upvotes: 2

Related Questions