wouldcloud图片操作出现AttributeError: shape. Did you mean: ‘save‘?报错

wouldcloud中出现AttributeError: shape. Did you mean: ‘save’?的问题。

在针对数据库Mysql中表的某一列数值进行wouldcloud图片操作时,出现以下状况。
在这里插入图片描述
首先进行网上查找原因,大多数说是由于导入图片的路径问题,仔细检查之后路径并没有错,相对路径如下。

img = Image.open(r'static/dos/images/m.jpg')

此外,在代码中还发现了其它错误:

cur = con.cursor()
sql = 'select remark from t_doubantop'
cur.execute(sql)
data = cur.fetchall()
text = ''
for item in data:
    text = text+item['remark']
cur.close()
con.close()

这里数据库所赋予remark的值是汉字,但是这里仍然出现类型出错,于是更改为:

text = text + str(item['remark'])

其次,在图片操作时也出现了问题:

img = Image.open(r'static/dos/images/m.jpg')
a = np.array(img)#将图片转换为数组
wc = WordCloud(
    background_color='white',
    mask=img,
    font_path='ITCBLKAD.TTF'

)

这里需要将mask=img改为mask=a。需要注意的是,在这里font_path='ITCBLKAD.TTF’中’ITCBLKAD.TTF’所代表的语言需要是英文,因为wordcloud的默认字体不支持中文。否则输出图片将会显示成方框状态,类似于此种状态。当然,解决此状态可以直接将支持中文的字体的路径传给 font_path。

font = r'C:\Windows\Fonts\simfang.ttf'

在这里插入图片描述
最后一个更改的问题是在此代码中:

plt.show()
plt.savefig(r'w.jpeg',dpi=500)#dpi分辨率

将两行代码的顺序交换,展示图片和保存图片的顺序交换。

源代码

from matplotlib import pyplot as plt
import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import pymysql

con = pymysql.connect(host='localhost',
                      user='root',
                      password='12345',
                      db='sys',
                      charset='utf8mb4',
                      cursorclass=pymysql.cursors.DictCursor)
cur = con.cursor()
sql = 'select remark from t_doubantop'
cur.execute(sql)
data = cur.fetchall()
text = ''
for item in data:
    text = text+item['remark']
cur.close()
con.close()
cut = jieba.cut(text)
string = ' '.join(cut)#将输出的字符串分开打印
img = Image.open(r'static/dos/images/m.jpg')
a = np.array(img)#将图片转换为数组
wc = WordCloud(
    background_color='white',
    mask=img,
    font_path='ITCBLKAD.TTF'

)
wc.generate_from_text(string)

fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')
plt.show()
plt.savefig(r'w.jpeg',dpi=500)#dpi分辨率

更改后的代码为

from matplotlib import pyplot as plt
import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import pymysql

con = pymysql.connect(host='localhost',
                      user='root',
                      password='12345',
                      db='sys',
                      charset='utf8mb4',
                      cursorclass=pymysql.cursors.DictCursor)
cur = con.cursor()
sql = 'select remark from t_doubantop'
cur.execute(sql)
data = cur.fetchall()
text = ''
for item in data:
    text = text + str(item['remark'])
cur.close()
con.close()
cut = jieba.cut(text)
string = ' '.join(cut)
img = Image.open(r'static/dos/images/m.jpg')
a = np.array(img)
wc = WordCloud(
    background_color='white',
    mask=a,
    font_path='ITCBLKAD.TTF'
)
wc.generate_from_text(string)

fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')
plt.savefig(r'w.jpeg',dpi=500)
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值