IOError:读取图像文件时数据流损坏

时间:2017-06-01 15:08:45

标签: python python-2.7 python-imaging-library

这是代码(一个简单的python代码,可以为图片添加一个数字):

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

我不知道真正的问题,从谷歌和stackoverflow的其他问题我找不到可以解决这个问题的方法。 谢谢!!

1 个答案:

答案 0 :(得分:1)

这是一个已知的issue。尝试使用pip install Pillow --upgrade

更新枕头