Python 尝试使用PIL保存图像时的索引器

Python 尝试使用PIL保存图像时的索引器,python,python-imaging-library,Python,Python Imaging Library,我试图将用户提交的PNG图像转换为JPEG格式,但当我试图保存图像时,我得到了 <type 'exceptions.IndexError'>: string index out of range 错误发生在p.save()行。我认为这是一个权限问题,但我甚至对文件/目录授予了777个权限,但仍然无法工作 编辑 这是save()调用之后的内容 保存中的/usr/lib/python2.7/dist-packages/PIL/Image.py(self=,fp='../files/o

我试图将用户提交的PNG图像转换为JPEG格式,但当我试图保存图像时,我得到了

<type 'exceptions.IndexError'>: string index out of range 
错误发生在p.save()行。我认为这是一个权限问题,但我甚至对文件/目录授予了777个权限,但仍然无法工作

编辑 这是save()调用之后的内容

保存中的
/usr/lib/python2.7/dist-packages/PIL/Image.py(self=,fp='../files/outfile.jpg',format='JPEG',**params={})
1434
1435可能会变异自我!
=>1436自加载()
1437
1438 self.encoderinfo=参数
self=,self.load=
/加载中的usr/lib/python2.7/dist-packages/PIL/ImageFile.py(self=)
204休息
205其他:
=>206提升索引器(ie)
207
208如果不是s:#截短的jpeg
内置索引器=,ie=索引器('字符串索引超出范围',)

我会自己回答这个问题,因为雨果没有给出答案

默认情况下,有一个名为LOAD\u TRUNCATED\u IMAGES的标志设置为False


您必须转到/usr/lib/python2.7/dist packages/PIL/ImageFile.py并将其设置为True

请提供更多堆栈跟踪线。你试过使用绝对路径吗?在问题中添加了跟踪。我也试过使用绝对路径。没有任何更改。它会引发错误,因为PNG被截断。您可以尝试编辑PIL的ImageFile.py并将
LOAD\u TRUNCATED\u IMAGES
设置为
True
。或者,为什么PNG会被截断?其他PNG是否也会发生同样的情况?您可以在其他图像查看器中打开它们吗?枕头而不是PIL也会发生同样的情况吗?我第一次编辑了错误的文件。现在我将LOAD_TRUNCATED_IMAGES设置为True,它就可以工作了。谢谢请将此作为答案发布。
if imghdr.what(filePath) == 'png':
    p = Image.open(filePath)
    p.save('../files/outfile.jpg', "JPEG")
    filePath = "../files/outfile.jpg"
 /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',)