Python 为什么我得到一个索引器:字符串索引超出范围?
我在Ubuntu11.10、Python2.7.2+上运行以下代码Python 为什么我得到一个索引器:字符串索引超出范围?,python,string,Python,String,我在Ubuntu11.10、Python2.7.2+上运行以下代码 import urllib import Image import StringIO source = '/home/cah/Downloads/evil2.gfx' dataFile = open(source, 'rb').read() slicedFile1 = StringIO.StringIO(dataFile[::5]) slicedFile2 = StringIO.StringIO(dataFile[1::5])
import urllib
import Image
import StringIO
source = '/home/cah/Downloads/evil2.gfx'
dataFile = open(source, 'rb').read()
slicedFile1 = StringIO.StringIO(dataFile[::5])
slicedFile2 = StringIO.StringIO(dataFile[1::5])
slicedFile3 = StringIO.StringIO(dataFile[2::5])
slicedFile4 = StringIO.StringIO(dataFile[3::5])
jpgimage1 = Image.open(slicedFile1)
jpgimage1.save('/home/cah/Documents/pychallenge12.1.jpg')
pngimage1 = Image.open(slicedFile2)
pngimage1.save('/home/cah/Documents/pychallenge12.2.png')
gifimage1 = Image.open(slicedFile3)
gifimage1.save('/home/cah/Documents/pychallenge12.3.gif')
pngimage2 = Image.open(slicedFile4)
pngimage2.save('/home/cah/Documents/pychallenge12.4.png')
本质上,我使用的是一个.bin文件,其中有多个图像文件的十六进制代码混乱像123451234512345。。。聚集在一起然后保存。问题是我遇到了以下错误:
File "/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 96, in read
len = i32(s)
File "/usr/lib/python2.7/dist-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
文件“/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py”,第96行,已读
len=i32(s)
i32第44行的文件“/usr/lib/python2.7/dist packages/PIL/PngImagePlugin.py”
返回ord(c[3])+(ord(c[2])在这个阶段,似乎len(s)<4
len = i32(s)
也就是说
s = self.fp.read(8)
不是读取全部4个字节吗
可能您传递的fp中的数据对图像解码器没有意义
仔细检查切片是否正确确保传递的字符串长度至少为4。错误不在于PIL,而在于切片。确认切片是否正确。应该有4个文件还是5个文件?那么我可以做些具体的事情吗?谢谢!对不起,我仍然不确定我应该做什么我要做些什么来修理它。
s = self.fp.read(8)