My task gets some files with header + image content. After the header extraction and create the image.png which is recognized and properly opened. This program works on windows with python 2.7.9 and the latest version at the time of PIL
Afterwords the image is converted from png to jpeg. The code snippet:
im = Image.open("c:\\1\\rawfile.png")
im.save('c:\\1\\rawfile.jpeg',"JPEG")
Here appears the error (on the im.save() line), it only after the loading, if i do img.crop(x), img.rotate(x) the same error appears.
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
I've tried the LOAD_TRUNCATED_IMAGES set to YES and it didn't work. I've also tried absolute paths with no luck.
On a debug stand alone program using the same hardcoded paths on the same files it works! (files are created, converted and properly read by file editors)
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'
If I hardcode the file paths on the main project it will fail and give the IndexError.