Python图像库-索引器错误:字符串索引超出范围|相同的代码一个有效,另一个无效
我的任务获取一些带有标题+图像内容的文件。标题提取后,创建可识别并正确打开的image.png。该程序在windows上使用python 2.7.9和PIL时的最新版本 后记:图像从png转换为jpeg。 代码片段:Python图像库-索引器错误:字符串索引超出范围|相同的代码一个有效,另一个无效,python,image-processing,python-imaging-library,pillow,Python,Image Processing,Python Imaging Library,Pillow,我的任务获取一些带有标题+图像内容的文件。标题提取后,创建可识别并正确打开的image.png。该程序在windows上使用python 2.7.9和PIL时的最新版本 后记:图像从png转换为jpeg。 代码片段: im = Image.open("c:\\1\\rawfile.png") im.save('c:\\1\\rawfile.jpeg',"JPEG") 这里出现了错误(在im.save()行上),只有在加载之后,如果我执行img.crop(x)、img.rotate(x),同样的
im = Image.open("c:\\1\\rawfile.png")
im.save('c:\\1\\rawfile.jpeg',"JPEG")
这里出现了错误(在im.save()行上),只有在加载之后,如果我执行img.crop(x)、img.rotate(x),同样的错误才会出现
Traceback (most recent call last):
File "getMail.py", line 225, in <module>
start_deamon()
File "getMail.py", line 217, in start_deamon
deamon.process_email()
File "getMail.py", line 114, in process_email
self.img_conv.convert_file('c:\\1\\rawfile\\rawfile.png', 'c:\\1\\rawfile\\rawfile.jpg' )
File "getMail.py", line 162, in convert_file
im.save('c:\\1\\rawfile.jpeg',"JPEG")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 198, in load
s = read(self.decodermaxblock)
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 391, in load_read
cid, pos, len = self.png.read()
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 96, in read
len = i32(s)
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 44, in i32
return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
IndexError: string index out of range
如果我硬编码主项目上的文件路径,它将失败并给出索引器。使用的是原始PIL。相反,我已经升级到枕头(PIL叉子),它用下面的代码解决了这个问题。()
请显示整个堆栈跟踪,而不仅仅是最后一行的错误。完成!没有添加它,因为它是在PIL电话上,如所述。但是更多的信息现在在堆栈上可用。
try:
with open( 'c:\\1\\rawFile', 'rb') as fin:
data = fin.read()
fin.close()
except:
print 'error1'
#do my stuff here
try:
with open( 'c:\\1\\rawfile.png', 'wb') as fout:
fout.write(data[index:])
fout.close()
except:
print 'error2'
try:
Image.open('c:\\1\\rawfile.png').save('c:\\1\\rawfile.jpg')
except:
print 'error 3'
from PIL import ImageFile
#To ensure that *.png file are read
ImageFile.LOAD_TRUNCATED_IMAGES = True