解决:cannot import name ‘imread’ from ‘scipy.misc’

比如这段代码现在出现ImportError: cannot import name ‘imread’ from ‘scipy.misc’

#GovRptWordCloudv1.py
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("fivestart.png")
f = open("新时代中国特色社会主义.txt", "r", encoding = "utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(font_path = "msyh.ttc", mask = mask, width = 1000, height = 700, background_color = "white")
w.generate(txt)
w.to_file("grwordcloud.png")
ImportError: cannot import name 'imread' from 'scipy.misc'

解决方法:

scipy已经将imread等命令删除,官方文档中有说明
“Functions from scipy.interpolate (spleval, spline, splmake, and spltopp) and functions from scipy.misc (bytescale, fromimage, imfilter, imread, imresize, imrotate, imsave, imshow, toimage) have been removed. The former set has been deprecated since v0.19.0 and the latter has been deprecated since v1.0.0. ”
我们需要换其他的库使用
如使用另一个库imageio

import imageio
imageio.imread()

如果没安要先安一下
解决:cannot import name ‘imread’ from ‘scipy.misc’_第1张图片

#GovRptWordCloudv1.py
import jieba
import wordcloud
import imageio
mask = imageio.imread("fivestart.png")
f = open("新时代中国特色社会主义.txt", "r", encoding = "utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(font_path = "msyh.ttc", mask = mask, width = 1000, height = 700, background_color = "white")
w.generate(txt)
w.to_file("grwordcloud.png")

再运行下代码就没问题了
解决:cannot import name ‘imread’ from ‘scipy.misc’_第2张图片
解决:cannot import name ‘imread’ from ‘scipy.misc’_第3张图片

你可能感兴趣的:(Python语言程序设计,opencv,python)