我正在尝试将用户提交的PNG图像转换为JPEG格式,但当我尝试保存图像时,出现以下错误:
我在Apache上使用CGI运行Python脚本。当我在控制台中运行脚本时,它可以正常工作。
以下是代码。
错误发生在p.save()这一行。我以为这是权限问题,但即使我在files/目录上给予777权限也无法解决。
编辑:这是save()调用之后的内容。
<type 'exceptions.IndexError'>: string index out of range
我在Apache上使用CGI运行Python脚本。当我在控制台中运行脚本时,它可以正常工作。
以下是代码。
if imghdr.what(filePath) == 'png':
p = Image.open(filePath)
p.save('../files/outfile.jpg', "JPEG")
filePath = "../files/outfile.jpg"
错误发生在p.save()这一行。我以为这是权限问题,但即使我在files/目录上给予777权限也无法解决。
编辑:这是save()调用之后的内容。
/usr/lib/python2.7/dist-packages/PIL/Image.py in save(self=<PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512>, fp='../files/outfile.jpg', format='JPEG', **params={})
1434
1435 # may mutate self!
=> 1436 self.load()
1437
1438 self.encoderinfo = params
self = <PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512>, self.load = <bound method PngImageFile.load of <PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512>>
/usr/lib/python2.7/dist-packages/PIL/ImageFile.py in load(self=<PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512>)
204 break
205 else:
=> 206 raise IndexError(ie)
207
208 if not s: # truncated jpeg
builtin IndexError = <type 'exceptions.IndexError'>, ie = IndexError('string index out of range',)
LOAD_TRUNCATED_IMAGES
设置为True
。或者,为什么PNG被截断了?其他PNG是否也会发生同样的情况?您能在其他图像查看器中打开它们吗?使用Pillow而不是PIL是否会发生相同的情况? - Hugo