Skip to content Skip to sidebar Skip to footer

Python/django Png,gif To Jpg

i'm using Django 1.6.2 and Python 3.3.5 and Pillow 2.3.0. What is the best way to convert an png/gif image to an jpg image in Django, so that the output-file is nearly the same as

Solution 1:

Use:

from PIL import Image
im = Image.open("file.png")
bg = Image.new("RGB", im.size, (255,255,255))
bg.paste(im, (0,0), im)
bg.save("file.jpg", quality=95)
  • Passing in the second image in bg.paste(im, (0,0), im) allows im's alpha channel to act as a mask over your background image.
  • The coordinates (0,0) paste your image perfectly over your background
  • bg.save("file.jpg", quality=95); quality=95 ensures the highest quality from PIL

Post a Comment for "Python/django Png,gif To Jpg"