gpt4 book ai didi

python图像库无法将PNG保存为JPG

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:36 26 4
gpt4 key购买 nike

我在 Mac OSX 上从源代码安装了 PIL 1.1.7。我还从 Macports 安装了所需的库。使用 python 2.6。

安装PIL后,我可以成功运行selftest.py(所有测试通过)

但是当我尝试运行以下代码时,我发现我无法将 PNG 转换为 JPG。但是,我能够将 PNG 转换为 GIF,将 PNG 转换为 PPM。我还可以阅读 JPG。

import os
import sys
import Image

for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
print outfile
if infile != outfile:
try:
Image.open(infile).save(outfile, 'jpg') #Note: gif or ppm works
except IOError:
print "cannot convert", infile
if os.path.exists(outfile):
print 'cleaning up...'
os.remove(outfile)

** 使用堆栈跟踪更新。看起来编码器丢失了。但是我确实通过 macport 安装了 libjpeg。

清理...

Traceback (most recent call last):
File "convert_to_jpeg.py", line 15, in <module>
Image.open(infile).save(outfile, 'jpeg')
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/Image.py", line 1439, in save
save_handler(self, fp, filename)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/JpegImagePlugin.py", line 471, in _save
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/ImageFile.py", line 495, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/Image.py", line 401, in _getencoder
raise IOError("encoder %s not available" % encoder_name)
IOError: encoder jpeg not available

最佳答案

使用 jpeg 而不是 jpg 作为格式标识符。

关于python图像库无法将PNG保存为JPG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8172098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com