猫史档案馆


五一专辑教程四

用户:萌新大佬萌新大佬查看:0 回复:0 评论:0 创建时间:2020-05-04T17:39:06


欢迎回来咱们开始吧

 

1.形状

上节课的图片下载了吧,先导入库

import imageio

看看图片是不是在代码的同一目录下下载的。取名叫“1.jpg”要注意“.jpg”后缀名

好了吗,好了打代码:

import wordcloud
import imageio
mask = imageio.imread("1.jpg")

font = "anna.ttf"

w = wordcloud.WordCloud(width=600, height=400,
                        background_color="white", font_path=font, mask=mask)

txt = "Come and buy your clothes at our great \
sale! We sell all our clothes at very good prices. \
Do you like sweaters? We have green sweaters for only $15! \
Yellow sweaters are only $12!Do you need trousers? \
For boys, we have black trousers for only $22. \
And shorts are only $16! For girls, we have skirts in purple for only $20. \
How much are our jackets? Only $30! And we have black shoes for only $28. \
Socks are only $2 for three pairs! Come to Mr. Cool’s Clothes Store now!"


w.generate(txt)

w.to_file("txt.png")

center_image

不太像emotion_星能猫_哼改代码

w = wordcloud.WordCloud(width=600, height=400, background_color="white",
                        font_path=font, max_font_size=100, mask=mask)

center_image

好多了

 

2.文件

把刚刚的课文存到与代码同一目录下center_imagecenter_image

改代码

import wordcloud
import imageio
mask = imageio.imread("1.jpg")

font = "anna.ttf"

w = wordcloud.WordCloud(width=600, height=400, background_color="white",
                        font_path=font, max_font_size=100, mask=mask)

f = open("txt.txt", "r")

txt = f.read()
f.close()


w.generate(txt)

w.to_file("txt.png")

记得一定一定一定一定一定要打f.close()要不然文档就废了!!!

结果:报错?!emotion_编程猫_紧张

我来教你:center_image

center_image

center_image

改代码

f = open("txt.txt", "r", encoding="UTF-8")
#                        加      这     个

搞定!!emotion_编程猫_点赞

┏(^0^)┛拜拜,明天见!


回复

上一页1 页 / 共 0下一页