0

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.

2
  • 1
    Please show the whole stack trace and not just the error from the last line. Commented Mar 20, 2015 at 17:05
  • Done! Didn't added it because it's upon the PIL call like described. But more info is available on the stack now. Commented Mar 23, 2015 at 8:10

1 Answer 1

2

The original PIL was being used. Instead I've upgraded to Pillow (a PIL fork) which with the following code solved the problem. (as already described)

from PIL import ImageFile

#To ensure that *.png file are read
ImageFile.LOAD_TRUNCATED_IMAGES = True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.