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.
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:
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.