微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何对某些图像进行图像处理时出现问题-错误:AttributeError:类型对象'Image'没有属性'open

如何解决如何对某些图像进行图像处理时出现问题-错误:AttributeError:类型对象'Image'没有属性'open

使用python对某些图像执行图像处理时出现错误错误是:

“ AttributeError:类型对象'Image'没有属性'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,font=font)

    img.save(outfile)

    display(Image(filename=outfile))
    

解决方法

注意!您在“相同名称”下有2个导入,彼此重叠。 我会进行更改:

#mabe you replace the original import to a more nutral name..
import PIL.Image as Image

from PIL import ImageFont

from PIL import ImageDraw

from IPython.display import Image as DisplayImage # this line fixes your problem

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,font=font)

    img.save(outfile)

    display(Image(filename=outfile))

下次,请不要混淆自己!选择一个更昵称的名称,然后选择图像,并且不会重复:)

***已编辑!***

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。