Python 如何对某些图像进行图像处理的问题-错误:AttributeError:type object';图像';没有属性';打开
我在使用python对某些图像执行图像处理时出错。错误是: AttributeError:类型对象“Image”没有属性“open” 如果您能查看下面的代码并帮助我修复此错误,我将非常高兴Python 如何对某些图像进行图像处理的问题-错误:AttributeError:type object';图像';没有属性';打开,python,image-processing,Python,Image Processing,我在使用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/m
#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))
#在图像上压印文本
从PIL导入图像
从PIL导入ImageFont
从PIL导入ImageDraw
从IPython.display导入图像
列表2=['mydata/marketst670503.jpg'、'mydata/marketst8407.jpg'、'mydata/potsdamriot6805.jpg'、'mydata/saure671221a.jpg']
outfile='sample text.jpg'
对于清单2中的行:
打印(行)
img=图像。打开(行)
draw=ImageDraw.draw(img)
#font=ImageFont.truetype(,)
font=ImageFont.truetype(“columbia.ttf”,200)
draw.text((0,0),“示例文本”,(255,0,0),font=font)
图像保存(输出文件)
显示(图像(文件名=输出文件))
注意!您有两个同名的导入,相互覆盖。
我会做出这样的改变:
#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,0,0),font=font)
img.save(outfile)
display(Image(filename=outfile))
#是否可以将原始导入内容替换为更具营养价值的名称。。
将PIL.Image作为图像导入
从PIL导入ImageFont
从PIL导入ImageDraw
从IPython.display导入图像为DisplayImage#这一行解决了您的问题
列表2=['mydata/marketst670503.jpg'、'mydata/marketst8407.jpg'、'mydata/potsdamriot6805.jpg'、'mydata/saure671221a.jpg']
outfile='sample text.jpg'
对于清单2中的行:
打印(行)
img=图像。打开(行)
draw=ImageDraw.draw(img)
#font=ImageFont.truetype(,)
font=ImageFont.truetype(“columbia.ttf”,200)
draw.text((0,0),“示例文本”,(255,0,0),font=font)
图像保存(输出文件)
显示(图像(文件名=输出文件))
下一次,不要把自己搞糊涂了!选择一个更具营养的名称,然后选择图像,这样就不会有重复:)
***编辑*** 您正在导入两个名称相同的模块“from IPython.display import Image”,“from PIL import Image”,这可能会导致问题。在运行代码后,我遇到了错误“ImportError:无法导入名称”'DisplayImage'。我试图安装“DisplayImage”“包,但不起作用。您知道如何修复此错误吗?@Elwakdy我认为您应该执行“从IPython.display导入图像为DisplayImage”,然后执行DisplayImage(文件名…)而不是display(图像(文件名…)。因为我认为模块没有DisplayImage方法。至少我在文件里找不到嘿,我犯了个愚蠢的错误,谢谢!注意编辑,我希望您导入图像,并使用'as'关键字将其命名为DisplayImage。现在,每次要使用来自Ipython.Display库的图像时,请使用此名称。