Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python IOError:读取图像文件时数据流中断_Python_Python 2.7_Python Imaging Library - Fatal编程技术网

Python IOError:读取图像文件时数据流中断

Python IOError:读取图像文件时数据流中断,python,python-2.7,python-imaging-library,Python,Python 2.7,Python Imaging Library,下面是代码(一个简单的python代码,可以为图片添加一个数字): 从PIL导入图像、ImageDraw、ImageFont def addnum(编号、文件路径): img=Image.open(文件路径) 宽度、高度=img.size draw=ImageDraw.draw(img) 正面尺寸=高度/4 ttf_front=ImageFont.truetype('Arial.ttf',front_size) draw.text((宽度-正面大小,0),数字,(255,0,0),字体=ttf\

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

从PIL导入图像、ImageDraw、ImageFont
def addnum(编号、文件路径):
img=Image.open(文件路径)
宽度、高度=img.size
draw=ImageDraw.draw(img)
正面尺寸=高度/4
ttf_front=ImageFont.truetype('Arial.ttf',front_size)
draw.text((宽度-正面大小,0),数字,(255,0,0),字体=ttf\U正面)
德拉鲁
img.save('wechat_addnum.jpg')
img.show()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
addnum('4','wechat.jpg')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Users/JourneyWoo/anaconda/lib/python2.7/site packages/spyder/utils/site/sitecustomize.py”,第866行,在runfile中
execfile(文件名、命名空间)
文件“/Users/JourneyWoo/anaconda/lib/python2.7/site packages/spyder/utils/site/sitecustomize.py”,第94行,在execfile中
execfile(文件名,*其中)
文件“/Users/JourneyWoo/Python/python100/python001/python000.py”,第27行,在
addnum('4','wechat.jpg')
addnum中第16行的文件“/Users/JourneyWoo/Python/python100/python001/python000.py”
draw=ImageDraw.draw(img)
文件“/Users/JourneyWoo/anaconda/lib/python2.7/site packages/PIL/ImageDraw.py”,第299行,在Draw中
返回图像绘制(im,模式)
文件“/Users/JourneyWoo/anaconda/lib/python2.7/site packages/PIL/ImageDraw.py”,第60行,在__
im.load()
文件“/Users/JourneyWoo/anaconda/lib/python2.7/site packages/PIL/ImageFile.py”,第244行,已加载
raise_ioerror(错误代码)
文件“/Users/JourneyWoo/anaconda/lib/python2.7/site packages/PIL/ImageFile.py”,第59行,在raise_ioerror中
引发IOError(消息+“读取图像文件时”)
IOError:读取图像文件时数据流中断
我不知道真正的问题,从谷歌和stackoverflow的其他问题中,我找不到解决这个问题的方法。
谢谢

这是一个常识。尝试使用
pip安装枕头更新枕头--升级

你能提出你的问题来显示确切的堆栈位置吗?同意,我们需要回溯来告诉你更多。谢谢,我已经更新了QQ,你应该尝试使用另一个图像。我从我的库中取了一张图片,效果很好(png)。另外:
font\u size=height//4
使用整数除法,否则代码在python 3中无法移植。有时字体大小不可用,您会遇到资源错误。。。
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