Python 使用 Pillow 将 png 转换为 jpeg

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43258461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:43:20  来源:igfitidea点击:

Convert png to jpeg using Pillow

pythonpython-3.ximagepython-imaging-library

提问by alex

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.

我正在尝试使用枕头将 png 转换为 jpeg。我已经尝试了几个脚本没有成功。这两个似乎适用于像这样的小 png 图像。

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?

我究竟做错了什么?

回答by dm2013

You should use convert() method:

您应该使用 convert() 方法:

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

更多信息:http: //pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.convert

回答by Jeremy S.

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

该图像的问题不在于它很大,而在于它不是 RGB,特别是它是一个索引图像。 在此处输入图片说明

Here's how I converted it using the shell:

以下是我使用 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')

回答by Mani

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

您可以将打开的图像转换为 RGB,然后您可以将其保存为任何格式。代码将是:

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.

然后转换为RGB并保存。

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.

您的代码的问题在于您将 png 粘贴到 RGB 块中并通过硬编码将其保存为 jpeg。您实际上并没有将 png 转换为 jpeg。

回答by Mohideen bin Mohammed

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

就是这样..您调整大小和转换后的图像将存储在同一位置