Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用枕头将png转换为jpeg_Python_Python 3.x_Image_Python Imaging Library - Fatal编程技术网

Python 使用枕头将png转换为jpeg

Python 使用枕头将png转换为jpeg,python,python-3.x,image,python-imaging-library,Python,Python 3.x,Image,Python Imaging Library,我正在尝试使用枕头将png转换为jpeg。我试了好几张纸条都没有成功。这两张照片似乎可以处理像这样的小png图像 第一个代码: 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") 第二个代码: image = Image

我正在尝试使用枕头将png转换为jpeg。我试了好几张纸条都没有成功。这两张照片似乎可以处理像这样的小png图像

第一个代码:

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")
第二个代码:

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)
但是如果我试着转换像这样的大图像

我要走了

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
回溯(最近一次呼叫最后一次):
文件“png_converter.py”,第14行,在
背景粘贴(图像,(0,0),图像)
文件“/usr/lib/python2.7/dist packages/PIL/Image.py”,第1328行,粘贴
self.im.paste(im,box,mask.im)value错误:错误的透明度掩码

我做错了什么?

您可以将打开的图像转换为RGB,然后以任何格式保存。守则如下:

from PIL import Image
im = Image.open("image_path")
im.convert('RGB').save("image_name.jpg","JPEG") #this converts png image as jpeg
如果要自定义图像大小,只需在如下方式打开时调整图像大小:

im = Image.open("image_path").resize(x,y)
然后转换为RGB并保存

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

您应该使用convert()方法:


更多信息:

该图像的问题不在于它太大,而在于它不是RGB,特别是它是一个索引图像。

下面是我如何使用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)
因此,在代码中添加图像模式检查:

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

如果要转换并调整大小,请尝试以下操作:

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

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

您尝试过吗?是的,我尝试过,这是第一个示例所有答案都很好,谢谢,但您有什么想法可以压缩大小吗?保存图像时尝试optimize=True。但这会使图像的白色背景变为白色。有没有办法修复它。@lifeisshubh白色应该转换成黑色吗?!我笑得很厉害@你的意思是“这是把图像的白色背景变成黑色吗?”@frank我是说你写的。据我记忆所及,它已经解决了。但是已经有很长一段时间了,我既没有我工作的那个系统,也没有那个代码。时间会使事物褪色。
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')