Convert png to jpeg using Pillow

118,635

Solution 1

You should use convert() method:

from PIL import Image

im = Image.open("Ba_b_do8mag_c6_big.png")
rgb_im = im.convert('RGB')
rgb_im.save('colors.jpg')

more info: http://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.convert

Solution 2

The issue with that image isn't that it's large, it is that it isn't RGB, specifically that it's an index image. enter image description here

Here's how I converted it using the shell:

>>> from PIL import Image
>>> im = Image.open("Ba_b_do8mag_c6_big.png")
>>> im.mode
'P'
>>> im = im.convert('RGB')
>>> im.mode
'RGB'
>>> im.save('im_as_jpg.jpg', quality=95)

So add a check for the mode of the image in your code:

if not im.mode == 'RGB':
  im = im.convert('RGB')

Solution 3

You can convert the opened image as RGB and then you can save it in any format. The code will be:

from PIL import Image
im = Image.open("image_path")
im.convert('RGB').save("image_name.jpg","JPEG") #this converts png image as jpeg

If you want custom size of the image just resize the image while opening like this:

im = Image.open("image_path").resize(x,y)

and then convert to RGB and save it.

The problem with your code is that you are pasting the png into an RGB block and saving it as jpeg by hard coding. you are not actually converting a png to jpeg.

Solution 4

if you want to convert along with resize then try this,

from PIL import Image

img = i.open('3-D Tic-Tac-Toe (USA).png').resize((400,400)) # (x,y) pixels
img.convert("RGB").save('myimg.jpg')

thats it.. your resized and converted image will store in same location

Solution 5

The simplest way to convert from png to jpeg (using the pillow)

from PIL import Image
img = Image.open("image.png")
new_img = img.convert('RGB')
new_img.save('image.jpeg')

With resize in a specific size

from PIL import Image
img = Image.open("image.png").resize((320,320)) #Here (x,y) in pixels
new_img = img.convert('RGB')
new_img.save('image.jpeg')
Share:
118,635
alex
Author by

alex

Django and python developer.

Updated on July 21, 2022

Comments

  • alex
    alex almost 2 years

    I am trying to convert png to jpeg using pillow. I've tried several scrips without success. These 2 seemed to work on small png images like this one.

    enter image description here

    First code:

    from PIL import Image
    import os, sys
    
    im = Image.open("Ba_b_do8mag_c6_big.png")
    bg = Image.new("RGB", im.size, (255,255,255))
    bg.paste(im,im)
    bg.save("colors.jpg")
    

    Second code:

    image = Image.open('Ba_b_do8mag_c6_big.png')
    bg = Image.new('RGBA',image.size,(255,255,255))
    bg.paste(image,(0,0),image)
    bg.save("test.jpg", quality=95)
    

    But if I try to convert a bigger image like this one

    I'm getting

    Traceback (most recent call last):
      File "png_converter.py", line 14, in <module>
        bg.paste(image,(0,0),image)
      File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1328, in paste
        self.im.paste(im, box, mask.im) ValueError: bad transparency mask
    

    What am i doing wrong?

  • alex
    alex about 7 years
    all answers are good, thank you, but any ideea how you can compress the size aswell?
  • Mani
    Mani about 7 years
    Try optimize=True while saving the image.
  • lifeisshubh
    lifeisshubh about 5 years
    But it is making the white background of the image white. Is there any way to fix it.
  • Anna
    Anna over 4 years
    @lifeisshubh are white supposed to be converted into black?!
  • Nathan
    Nathan over 4 years
    I laughed so hard at this. @lifeisshubh Did you mean "it is making the white background of the image black?"
  • lifeisshubh
    lifeisshubh over 4 years
    @frank I mean what you have written.
  • Nathan
    Nathan over 4 years
    @lifeisshubh Can you replicate the bug? I'm sure they'd like to know, especially if a lot of people have the same issue. There might also be someone with your same issue who has fixed it :)
  • lifeisshubh
    lifeisshubh over 4 years
    As far as I remember it got resolved. But it's been a long time, I neither have that system on which I was working, nor that code. Time fades things away.
  • arun kumar
    arun kumar about 4 years
    Here colors.jpg store in particular local path can you give me an example?
  • Nathan
    Nathan about 4 years
    @arunkumar normally I'm not a stickler for grammar, but in this case I literally can't understand your comment. Are you saying the OP's code worked for you?
  • GBBL
    GBBL about 3 years
    I have a BIG issue when trying to convert large a grayscale 16bit PNG to compress it. The code is quite simple but all what I get is just a white sheet. The original large PNG file can be found at postimg.cc/p5PQG8ry the code is: from PIL import Image im = Image.open('terzafoto.png').convert('RGB') im.save('terzafoto.jpg', format='JPEG', quality=100)
  • default123
    default123 almost 3 years
    I had to do image.convert('RGB') followed by image.mode = 'RGB'