From d36e624acb349b3fd78bb3fb91ba0bcc696719c2 Mon Sep 17 00:00:00 2001 From: deepakprakash Date: Wed, 2 Nov 2011 14:25:22 +0530 Subject: [PATCH] Fix conversion of PNG "palette" or "P" mode images to JPEG. "P" mode images need to be converted to 'RGB' if target image format is not PNG or GIF. --- imagekit/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/imagekit/utils.py b/imagekit/utils.py index 637433e..2c72185 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -10,10 +10,11 @@ def img_to_fobj(img, format, **kwargs): tmp = tempfile.TemporaryFile() # Preserve transparency if the image is in Pallette (P) mode. - if img.mode == 'P': + transparency_formats = ('PNG', 'GIF', ) + if img.mode == 'P' and format in transparency_formats: kwargs['transparency'] = len(img.split()[-1].getcolors()) else: - img.convert('RGB') + img = img.convert('RGB') img.save(tmp, format, **kwargs) tmp.seek(0)