python'PngImageFile' object has no attribute 'shape'

xiaoxiao2021-02-28  61

我在生成词云是导入图片的时候冒出来这么个问题,在网上搜了一下,没有看到解决方法(也许是我的搜索姿势不对)

部分代码如下,

img = Image.open(path) wc = WordCloud(     background_color='white', width=1000, height=300, mask=img, font_path=font, stopwords=stopword ).generate_from_text(string)#绘制图片

报错的中文解释为:'属性' pngimagefile 对象没有形状    这句话我也不是很懂

不过我选择换一个图片导入方式,

img = plt.imread(path+r'\22.png')

他给我换了个报错方式,

 UserWarning: mask image should be unsigned byte between 0 and 255. Got a float array

  warnings.warn("mask image should be unsigned byte between 0"

不过这次我认识了,所以我机智的改了将图片改成数组

img_array = np.array(img)

结果他还是报那个错,然后我又改回了原来的图片导入方式,

img = Image.open(path) #打开图片 img_array = np.array(img) #将图片装换为数组 wc = WordCloud( background_color='white', width=1000, height=300, mask=img_array, font_path=font, ).generate_from_text(string)#绘制图片

然后他就成功了,他就有用了,哈哈哈哈!

具体原因我也不太清楚,欢迎大家解释,指正。

转载请注明原文地址: https://www.6miu.com/read-2622966.html

最新回复(0)