python圣诞树词云

news2024/10/6 8:23:12

一、前言

圣诞节虽然是西方节日,但是个人还是比较喜欢的(没有崇洋媚外的意思,中国的春节也超级棒),一个是圣诞节的氛围,圣诞节的圣诞老人等象征、雪花麋鹿等元素,都充满了浪漫的氛围。我想这也是其他很多人所喜欢的理由。当然,之于商家而言,可能商业营销的意味更重。
要说有意思的内容,C站上其实有很多,比如各种语言编写的圣诞树、圣诞网页等等。我觉得一个比较简单的方式是找一个圣诞元素的掩膜,可以将某篇自己喜欢的文章制作成词云,就很有意思。

二、创意名

如上所述,下面介绍一个圣诞树词云的制作方法。

三、效果展示

在这里插入图片描述

四、实现步骤

如上词云是通过python实现的。

  1. 首先安装几个需要的库:
pip install wordcloud PIL numpy jieba
  1. 下载文章保存成txt文本。这里的文本可以是中文的,也可以是英文的,上图所示的是英文版小红帽故事的词云结果。为了方便使用,我把它贴在博客的最后,可直接复制~
  2. 编写代码(见第五部分)
  3. 运行

五、编码实现

from wordcloud import WordCloud
import PIL.Image as image
import numpy as np

# 一些变量值,依据自己实际情况进行设置
edcoding_type = "utf-8"               # 编码方式     
background_color = "white"            # 生成图片的背景颜色
txt_path = "little red-cap.txt"       # 文本路径
mask_path = "mask.png"                # 词云形状掩码路径
img_path = "red-cap_wordcloud.png"    # 输出生成的词云图片路径
max_words = 200                       # 最大显示词数量

# 读取文本内容
def get_txt(txtpath):
    with open(txtpath, encoding = edcoding_type) as f:
        text = f.read()
    return text

# 生成词云
def generate_wordcloud(wordlist, maskpath, backgroundcolor, maxwords):
    mask = np.array(image.open(maskpath))                      # 设置图形掩码
    wordcloud = WordCloud(
        background_color = backgroundcolor,                    # 设置图片背景颜色
        mask = mask,                                           # 设置掩码
        max_words = maxwords                                   # 设置最大显示词数
    ).generate(wordlist)                                
    return wordcloud

text = get_txt(txt_path)                                       # 获取文本
word_cloud = generate_wordcloud(text,                          # 生成词云
                                mask_path,
                                background_color,
                                max_words)
image_file = word_cloud.to_image()                             # 生成图片
image_file.show()                                              # 显示图片
word_cloud.to_file(img_path)                                   # 保存生成的图片

六、相关资源

素材可以在这里下载:链接
英文版小红帽故事:
Once upon a time there was a dear little girl who was lovedby every one who looked at her, but most of all by hergrandmother, and there was nothing that she would not havegiven to the child. Once she gave her a little cap of redvelvet,which suited her so well that she would never wearanything else. So she was always called little red-cap.
One day her mother said to her,come,little red-cap,hereis a piece of cake and a bottle of wine. Take them to yourgrandmother, she is ill and weak, and they will do her good.Set out before it gets hot, and when you are going,walknicely and quietly and do not run off the path, or you mayfall and break the bottle, and then your grandmother willget nothing. And when you go into her room, don’t forgetto say,good-morning, and don’t peep into every cornerbefore you do it.
l will take great care, said little red-cap to her mother,and gave her hand on it.
l will take great care, said little red-cap to her mother,and gave her hand on it.
The grandmother lived out in the wood, half a league fromthe village,and just as little red-cap entered the wood, a wolf met her. Red-cap did not know what a wicked creaturehe was,and was not at all afraid of him.
"Good-day,little red-cap, " said he.
“Thank you kindly,wolf.”
“Wh ither away so early,little red-cap?”
"To my grandmother’s. "
“What have you got in your apron?”
"Cake and wine. Yesterday was baking-day,so poor sickgrandmother is to have something good,to make her stronger. "
“Where does your grandmother live, little red-cap?”
"A good quarter of a league farther on in the wood. Her housestands under the three large oak-trees,the nut-trees are just below. You surely must know it, " replied littlered-cap.
The wolf thought to himself, what a tender young creature. What a nice plump mouthful, she will be better to eat thanthe old woman. l must act craftily, so as to catch both.So he walked for a short time by the side of little red-cap,and then he said, "see little red-cap, how pretty theflowers are about here. Why do you not look round. l believe,too, that you do not hear how sweetly the little birds aresinging. You walk gravely along as if you were going toschool, while everything else out here in the wood is merry. "
Little red-cap raised her eyes,and when she saw thesunbeams dancing here and there through the trees,andpretty flowers growing everywhere, she thought,suppose ltake grandmother a fresh nosegay. That would please her too.lt is so early in the day that l shall still get there ingood time. And so she ran from the path into the wood tolook for flowers. And whenever she had picked one,shefancied that she saw a still prettier one farther on, andran after it, and so got deeper and deeper into the wood. Meanwhile the wolf ran straight to the grandmother’s houseand knocked at the door.
“Who is there?”
"Little red-cap, " replied the wolf. "She is bringing cakeand wine. Open the door. "
"Lift the latch, " called out the grandmother, " l am too weak,and cannot get up. "
The wolf lifted the latch, the door sprang open,and withoutsaying a word he went straight to the grandmother’s bed,and devoured her. Then he put on her clothes, dressedhimself in her cap,laid himself in bed and drew thecurtains.
Little red-cap,however,had been running about pickingflowers,and when she had gathered so many that she couldcarry no more,she remembered her grandmother,and set outon the way to her.
She was surprised to find the cottage-door standing open,and when she went into the room,she had such a strangefeeling that she said to herself,oh dear,how uneasy l feelto-day, and at other times l like being with grandmotherso much. She called out,"good morning, " but received no answer. So she went to the bed and drew back the curtains. There lay her grandmother with her cap pulled far over herface, and looking very strange.
"Oh, grandmother, " she said, “what big ears you have.” “Thebetter to hear you with,my child,” was the reply. "But,grandmother,what big eyes you have, " she said. "The betterto see you with, " my dear. “But,grandmother, what largehands you have.” “The better to hug you with.”“Oh, but,grandmother,what a terrible big mouth you have.” "Thebetter to eat you with. "
And scarcely had the wolf said this,than with one boundhe was out of bed and swallowed up red-cap.
When the wolf had appeased his appetite, he lay down againin the bed,fell asleep and began to snore very loud. Thehuntsman was just passing the house,and thought to himself,how the old woman is snoring. l must just see if she wantsanything.
So he went into the room,and when he came to the bed, hesaw that the wolf was lying in it. Do l find you here,youold sinner, said he. l have long sought you. Then just ashe was going to fire at him, it occurred to him that thewolf might have devoured the grandmother, and that shemight still be saved, so he did not fire, but took a pair of scissors,and began to cut open the stomach of thesleeping wolf. When he had made two snips, he saw the littlered-cap shining,and then he made two snips more, and thelittle girl sprang out, crying, ah, how frightened l have been. How dark it was inside the wolf.And after that the agedgrandmother came out alive also, but scarcely able to breathe.Red-cap, however, quickly fetched greatstones with which they filled the wolfs belly, and when he awoke, he wanted to run away, but the stones were so heavy that he collapsed at once, andfell dead.
Then all three were delighted.The huntsman drew off the wolf’s skin and went home with it.The grandmother ate the cake and drank the wine which red-cap had brought, and revived, but red-cap thoughtto herself, as long as llive, I will never by myself leave the path, to run into the wood, when my mother hasforbidden me to do so.
lt is also related that once when red-cap was again taking cakes to the old grandmother, another wolfspoke to her, and tried to entice her from the path. Red-cap, however, was on her guard, and went straightforward on her way, and told her grandmother that she had met the wolf, and that he had said good-morningto her, but with such a wicked look in his eyes, that if they had not been on the public road she was certainhe would have eaten her up. Well, said the grandmother, we will shut the door, that he may not come in.Soon afterwards the wolf knocked, and cried, open the door, grandmother, l am little red-cap, and ambringing you some cakes.But they did not speak, or open the door, so the grey-beard stole twice or thriceround the house, and at last jumped on the roof, intending to wait until red-cap went home in the evening.and then to steal after her and devour her in the darkness.But the grandmother saw what was in histhoughts. In front of the house was a great stone trough, so she said to the child, take the pail, red-cap.made some sausages yesterday, so carry the water in which l boiled them to the trough.Red-cap carrieuntil the great trough was quite full. Then the smell of the sausages reached the wolf, and he sniffed andpeeped down, and at last stretched out his neck so far that he could no longer keep his footing and began toslip, and slipped down from the roof straight into the great trough, and was drowned.But red-cap went joyously home, and no one ever did anything to harm her again.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/126089.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux的文件系统编程(1)

What makes the desert beautiful is that somewhere it hides a well. 沙漠之所以美丽,是因为在它的某个角落隐藏着一口井. Linux的文件系统编程(1)运行过程框架标准IO和文件IO标准IO文件IO(主要学)open函数两个参数三个参数close函数read函数write函数…

Python基础语法(二)

Python基础语法(二) 函数 编程中的函数和数学中的函数有一定的相似之处. 数学上的函数, 比如 y sin x , x 取不同的值, y 就会得到不同的结果. 编程中的函数, 是一段 可以被重复使用的代码片段 . 代码示例: 求数列的和, 不使用函数 # 1. 求 1 - 100 …

树Tree【代码笔记】

树【Tree】 树是n(n>0)个结点的有限集。当n 0时,称为空树。在任意一棵非空树中应满足: 有且仅有一个特定的称为根的结点。当n>1时,其余节点可分为m(m>0)个互不相交的有限集T1,T2,……

OpenHarmony#深入浅出学习eTs#(六)编写eTs第一个控件

本项目Gitee仓地址:深入浅出eTs学习: 带大家深入浅出学习eTs (gitee.com) 一、控件基本属性 在使用第一个控件前,我们需要了解一些控件都有哪些基础属性,比如说我们在Super Visual中使用过的长宽和字体大小等等,通用属性有以下这…

Retrofit的使用

文章目录Retrofit的使用最好用的网络库: RetrofitRetrofit的基本用法处理复杂接口的地址类型Retrofit构建器的最佳写法Retrofit的使用 最好用的网络库: Retrofit Retrofit是一款由Square公司开发的网络库,但是它和OkHttp定位完全不同,OkHttp的侧重点是底层通信的实现,而Retro…

Java集合类——LinkedList(单链表及双链表)

一,ArrayList的缺陷 1.空间浪费 在之前的博客中,我利用源码详细的讲解了ArrayList这个集合类(尤其是扩容机制),可以知道ArrayList的底层主要是一个动态的可变数组,容量满的时候需要进行1.5倍扩容。但是我…

第二十讲:神州路由器静态路由的配置

实验拓扑图如下所示 设备 端口 IP 子网掩码 网关 Router-A G0/0 120.83.200.55 255.255.255.0 无 G0/3 192.168.0.1 255.255.255.0 无 Router-B G0/0 120.83.200.56 255.255.255.0 无 G0/3 192.168.1.1 255.255.255.0 无 PC1 192.168.0.2 255.255.255…

jQuery 的基本使用

1、jQuery 介绍 1.1、JavaScript 库 JavaScript库:即 library,是一个封装好的特定的集合(方法和函数)。从封装一大堆函数的角度理解库,就是在这个库中,封装了很多预先定义好的函数在里面,比如动…

【C++】const关键字

【C】const关键字 0x1 常量 C定义常量有两种方式 #define 宏常量:#define 常量名 常量值 通常在文件上方定义,表示一个常量宏常量不可以修改 // 宏常量 #define MAX 999int main() {return 0; }const修饰的变量: const 数据类型 常量名 …

docker 安装Es

1、下载镜像文件 docker pull elasticsearch:7.4.2 存储和检索数据 docker pull kibana:7.4.2 可视化检索数据 2、创建实例 1、ElasticSearch mkdir -p /mydata/elasticsearch/config mkdir -p /mydata/elasticsearch/data echo "http.host: 0.0.0.0" >…

第三十六章 数论——容斥原理

第三十六章 数论——容斥原理一、容斥原理1、定理内容二、代码模板1、问题(1)如何求出能够被整除的个数?(2)如何枚举出2n−12^n-12n−1种情况?2、代码实现:一、容斥原理 1、定理内容 我们在高…

开启微信小程序的学习窗口(第一课)

第一个问题 什么是微信小程序 微信小程序,小程序的一种,英文名Wechat Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用。 全面开放申请后&#xff0…

Educational Codeforces Round 93 (Rated for Div. 2) K. Lonely Numbers

Problem - C - Codeforces 翻译: 给定一个数组𝑎1,𝑎2,…,𝑎𝑛,由0到9的整数组成。一子数组𝑎𝑙,𝑎𝑙 1,𝑎&…

R实战 | 置换多元方差分析(以PCoA的PERMANOVA分析为例)

adonis-cover置换多元方差分析(Permutational multivariate analysis of variance,PERMANOVA),又称非参数多因素方差分析(nonparametric multivariate analysis of variance)、或者ADONIS分析。它利用距离矩…

第003课 - 分布式基础概念

文章目录 集群、分布式、节点远程调用负载均衡服务注册/发现和注册中心服务熔断和降级API网关我们以前将所有的代码、页面、sql语句,写到一个应用,如果有一个地方有问题,整个就不可用了。 我们可以基于业务边界进行服务微化和拆分。 如果有一个出现了问题,不影响其他服务…

迅为LS2K0500开发板龙芯全国产处理器LoongArch架构核心主板

全国产开发板: 迅为iTOP-LS2K0500开发采用龙芯LS2K0500处理器,基于龙芯自主指令系统(LoongArch)架构,片内集成64位LA264处理器核、32位DDR3控制器、2DGPU、DVO显示接口、两路PCle2.0、两路SATA2.0、四路USB2.0、一路US…

梯度下降算法、随机梯度下降算法、动量随机梯度下降算法、AdaGrad算法、RMSProp算法、Adam算法详细介绍及其原理详解

相关文章 梯度下降算法、随机梯度下降算法、动量随机梯度下降算法、AdaGrad算法、RMSProp算法、Adam算法详细介绍及其原理详解反向传播算法和计算图详细介绍及其原理详解 文章目录相关文章前言一、回归拟合问题二、损失函数三、梯度下降算法四、随机梯度下降算法五、动量随机梯…

国际山岳日,周大福百年承诺续写永恒美好

纵横古今,俯瞰万里 每一寸绿野都孕育万物生机 每一座山林都彰示生命之本 百周年承诺 守护自然生态 周大福珠宝集团坚守“用真诚让幸福永恒“的企业理念 我们的百周年承诺包括对地球真诚且有效的付出服务 致力守护珍贵的大自然环境,为人类和星球幸福…

吉林优美姿文化:抖音怎么做爆款输出?

要知道,现在自媒体发展的越来越好了,其中发展的最好的就是抖音平台,大家如果要利用抖音平台达到引流的目的的话,也要去学习一下抖音相关的技巧,那么抖音怎么去买号呢?跟着吉林优美姿小编来一起看看吧&#…

亚马逊---人工智能入门---学习笔记

🚀write in front🚀 📝个人主页:认真写博客的夏目浅石. 🎁欢迎各位→点赞👍 收藏⭐️ 留言📝​ 📣系列专栏:蓝桥杯算法笔记 💬总结:希望你看完之…