Tozz
Tozz

Reputation: 276

IOError: broken data stream when reading image file

This is the code(a simple python code which could add a number to a picture):

from PIL import Image, ImageDraw, ImageFont

def addnum(number, filepath):
    img = Image.open(filepath)

    width, height = img.size

    draw = ImageDraw.Draw(img)

    front_size = height/4
    ttf_front = ImageFont.truetype('Arial.ttf', front_size)
    draw.text((width - front_size, 0), number, (255, 0, 0), font = ttf_front)

    del draw
    img.save('wechat_addnum.jpg')
    img.show()

if __name__ == '__main__':
    addnum('4','wechat.jpg')



Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/Users/JourneyWoo/Python/python100/python001/python000.py", line 27, in <module>
addnum('4','wechat.jpg')
File "/Users/JourneyWoo/Python/python100/python001/python000.py", line 16, in addnum
draw = ImageDraw.Draw(img)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageDraw.py", line 299, in Draw
return ImageDraw(im, mode)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageDraw.py", line 60, in __init__
im.load()
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageFile.py", line 244, in load
raise_ioerror(err_code)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file

I don't know the real problem, and from the google and other questions from stackoverflow I cannot find the method which could solve this problem. Thank you!!

Upvotes: 3

Views: 2372

Answers (1)

notGeek
notGeek

Reputation: 1528

This is a know issue. Try to update Pillow using pip install Pillow --upgrade

Upvotes: 1

Related Questions