0

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

Community
  • 1
  • 1
Mark K
  • 8,767
  • 14
  • 58
  • 118

1 Answers1

2

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')
Community
  • 1
  • 1
physicalattraction
  • 6,485
  • 10
  • 63
  • 122
  • thanks, physicalattraction. but it gives "ValueError: Not a valid number of quantization tables. Should be between 1 and 4." – Mark K Nov 10 '14 at 07:59