python圣诞树词云

一、前言

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

二、创意名

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

三、效果展示

python圣诞树词云_第1张图片

四、实现步骤

如上词云是通过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.

你可能感兴趣的:(python,python,开发语言)