当前位置:   article > 正文

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

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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
ImportError: cannot import name 'imread' from 'scipy.misc'
  • 1
解决方法:

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()
  • 1
  • 2

如果没安要先安一下
图

#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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

再运行下代码就没问题了
图1
图2

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/648631
推荐阅读
相关标签
  

闽ICP备14008679号