【发布时间】:2020-09-11 16:05:23
【问题描述】:
使用 python 对某些图像执行图像处理时出现错误。错误是:
"AttributeError: type object 'Image' has no attribute 'open'".
如果您能查看下面的代码并帮助我修复此错误,我会很高兴。
#imprint text on image
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from IPython.display import Image
list2 = ['mydata/marketst670503.jpg','mydata/marketst8407.jpg','mydata/potsdamriot6805.jpg','mydata/rescue671221a.jpg']
outfile = 'sample-text.jpg'
for line in list2:
print (line)
img = Image.open(line)
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("Colombia.ttf", 200)
draw.text((0, 0),"Sample Text",(255,0,0),font=font)
img.save(outfile)
display(Image(filename=outfile))
【问题讨论】:
-
您正在导入两个具有相同名称的模块“from IPython.display import Image”、“from PIL import Image”,这可能会导致问题。