Convert all images to JPEG

T

Thomas

Hi,

I got a bunch of different images of different types ( bmp, gif, png,
tiff etc ) and I want to convert them all to JPEGs using PIL. Is this
possible? When I try I get all sorts of errors, doing something like :

im = Image.open(srcImage) # might be png, gif etc, for instance
test1.png
im.thumbnail(size, Image.ANTIALIAS) # size is 640x480
im.save(targetName, "JPEG") # targetname is test1.jpg

produces an exception. Any clues?

Best regards,
Thomas
 
L

Leif K-Brooks

Thomas said:
im = Image.open(srcImage) # might be png, gif etc, for instance
test1.png
im.thumbnail(size, Image.ANTIALIAS) # size is 640x480
im.save(targetName, "JPEG") # targetname is test1.jpg

produces an exception. Any clues?

The problem is that test1.png is a paletted image, and JPEGs can only
hold true-color images. Try changing the last line to:

im.convert('RGB').save(targetName, 'JPEG')
 
F

Fredrik Lundh

Thomas said:
I got a bunch of different images of different types ( bmp, gif, png,
tiff etc ) and I want to convert them all to JPEGs using PIL. Is this
possible? When I try I get all sorts of errors, doing something like :

im = Image.open(srcImage) # might be png, gif etc, for instance
test1.png
im.thumbnail(size, Image.ANTIALIAS) # size is 640x480
im.save(targetName, "JPEG") # targetname is test1.jpg

produces an exception. Any clues?

the exception contains a pretty strong clue:

"IOError: cannot write mode P as JPEG"

adding

if im.mode != "RGB":
im = im.convert("RGB")

between the open and thumbnail calls fixes this.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top