tags:

views:

206

answers:

3

I have an image. I want to resize it using PIL, but it comes out like this.

Even without a resize, it still messes up the colour.

Minimal code:

from PIL import Image
import os
import urllib
import webbrowser

orig_url = 'http://mercedesclub.org.uk/images/stackoverflow-question/least-popular-colours-_-500-x-500.jpg'
temp_fn, _ = urllib.urlretrieve(orig_url)

im = Image.open(temp_fn)

fn = os.tempnam() + '.jpg'
im.save(fn)
webbrowser.open(fn)

I've tried Image.open(temp_fn).convert(format) with 'RGB', 'CMYK' and 'L' as formats, but still get weirdly coloured or grey results.

When I load the image from my hard drive and I can see:

>>>im.info  
{'adobe': 100, 
'progression': 1, 
'exif': 'Exif\x00\x00MM\x00*...\x7f\xff\xd9', 
'adobe_transform': 100}

>>>im.format  
'JPEG'  

>>>im.mode  
'CMYK'  

>>> im._getexif()
{40961: 65535, 40962: 500, 40963: 500, 296: 2, 34665: 164, 274: 1, 305: 'Adobe Photoshop CS Macintosh', 306: '2010:02:26 12:46:54', 282: (300, 1), 283: (300, 1)}

Thanks and let me know if you need any more data.

+2  A: 

PIL seems to have a problem loading some JPEG files in CMYK format. If you can convert the image to a more commonly supported color format (using another tool) it will help.

There's a PIL patch posted here, but I haven't tried it.

interjay
+1  A: 

I've had problems like this when the original image was saved in CMYK mode. I had to resave the image in RGB before processing it with PIL.

digitaldreamer
+3  A: 

Following interjay's link, the problem was fixed by upgrading to PIL 1.1.7. This includes an update to allow CMYK jpegs created by Photoshop to work correctly. But don't blame PIL, as Fredrik Lundh puts it:

"CMYK in JPEG is one big mess, mainly because Adobe got it wrong in Photoshop many years ago."

By the way, you can find which version of PIL you have by doing:

>>> Image.VERSION  
'1.1.7'  

Update: to make the resulting image display in IE you need to add .convert('RGB') so that you're outputting in standard jpeg RGB format, not CMYK.

Tom Viner