Daniel Soares
Daniel Soares

Reputation: 1

Error Python Imaging Library

I'm doing a system of change of avatar using the Python Imaging Library, but I'm having problem in following error presented:

client.changeAvatar(data)
  File "C:\Users\Administrator\Desktop\Main.py", line 332, in changeAvatar
    iM = i.resize((100, 100), Image.ANTIALIAS)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1262, in resize
    self.load()
  File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 192, in load
    raise IOError("image file is truncated (%d bytes not processed)" % len(b))
exceptions.IOError: image file is truncated (13 bytes not processed)

Code:

    with open("%i.jpg" %(self.playerID), "wb") as f:
        f.write(data)

    i = Image.open("%i.jpg" %(self.playerID))
    iM = i.resize((100, 100), Image.ANTIALIAS)
    iM.save("%i.jpg" %(self.playerID))

    im = i.resize((50, 50), Image.ANTIALIAS)
    im.save("%i_50.jpg" %(self.playerID))


    F = ftplib.FTP("", "", "")
    cdTree("/public_html/avatar/%s"%(self.playerID))

    for name in ["%i.jpg" %(self.playerID), "%i_50.jpg" %(self.playerID)]:
        with open(name, "rb") as fp:
            F.storbinary("STOR public_html/avatar/%s/%s" %(self.playerID, name), fp)
        os.system("del %s" %(name))

    F.quit()

Can anyone help me solve this error?

Upvotes: 0

Views: 1328

Answers (1)

johntellsall
johntellsall

Reputation: 15160

Once you have your Image object, re-use it to make the different thumbnails.

Take a look at the Image.thumbnail() function, it's a little easier to use than just downsampling the original image.

import PIL, PIL.Image
from StringIO import StringIO

data = open('rose.jpg').read()

img = PIL.Image.open( StringIO(data) )
playerID = 123

# write full image to file
with open("%i.jpg" %(playerID), "wb") as f:
    f.write(data)

# using same image, make 100px thumbnail
# TODO: use .thumbnail()
img.resize((100, 100), PIL.Image.ANTIALIAS
).save('%i_100.jpg' % playerID)

# make a 50px thumbnail
img.resize((50, 50), PIL.Image.ANTIALIAS
       ).save("%i_50.jpg" %playerID)

Upvotes: 1

Related Questions