Convert png to jpeg using Pillow

后端 未结 4 2018
陌清茗
陌清茗 2020-11-28 06:45

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.

First

4条回答
  •  一整个雨季
    2020-11-28 07:29

    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.

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

提交回复
热议问题