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.
This commit is contained in:
deepakprakash 2011-11-02 14:25:22 +05:30
parent 5985def06b
commit d36e624acb

View file

@ -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)