宣扬红色革命精神——文字云制作


前言

大家好!
红色教育是当今学生的学习生活中不可缺少的内容,作为一名少先队员,我也有宣传红色教育的义务。
在我看来,图片有时候比文字更有传播力,但是文字加上图片或者图片嵌上文字也许会有更好的传播能力,所以我想到了文字云。


环境准备

编辑器:pycharm64
运行环境:python3.7
所需库:
wordcloud pip install wordcloud
jieba pip install jieba
numpy pip install numpy
PIL 无需另外安装


文字云制作

纯文本文字云

首先,我们从基础文字云做起,首先,我们需要准备一个txt文件用于填充文字,以及一张空白的图片。
运行以下代码,即可输出一张文字云图片:

import wordcloud
import numpy as np
from PIL import Image  # Image模块是在Python PIL图像处理常用的模块
import jieba
import matplotlib.pyplot as plt


def word_cloud():
    text = open(r'文字路径![请添加图片描述](https://img-blog.csdnimg.cn/c867a66f07c84fe8bcfeb4b0095a556b.png)
', "r").read()  # 读入txt文本数据,在字符串前面加上字符r或R之后表示原始字符串,字符串中的任意字符都不再进行转义,后一个r表示“只读”
    cut_text = jieba.cut(text)  # 结巴中文分词,生成字符串,默认精确模式,如果不通过分词,无法直接生成正确的中文词云
    result = " ".join(cut_text)  # 必须给个符号分隔开分词结果来形成字符串,否则不能绘制词云
    wc = wordcloud.WordCloud(
        # 设置字体,不指定就会出现乱码
        background_color='white',  # 设置背景色,默认为黑色
        width=500,  # 设置背景宽
        height=350,  # 设置背景高
        max_font_size=50,  # 最大字体
        min_font_size=10,  # 最小字体
        mode='RGBA'  # 当参数为“RGBA”并且background_color不为空时,背景为透明
    )
    wc.generate(result)  # 根据分词后的文本产生词云
    wc.to_file(r"图片路径")  # 保存绘制好的词云图
    plt.imshow(wc)  # 以图片的形式显示词云
    plt.axis("off")  # 关闭图像坐标系,即不显示坐标系
    plt.show()  # plt.imshow()函数负责对图像进行处理,并显示其格式,但是不能显示。其后必须有plt.show()才能显示

word_cloud()

我们的txt文件选择了一篇英语文章:

Every single time you access a website, you leave tracks. Tracks that others can access. If you don't like the idea, find out what software can help you cover them.

Anti Tracks

Anti Tracks is a complete solution to protect your privacy and enhance your PC performance. With a simple click Anti Tracks securely erase your internet tracks, computer activities and programs history information stored in many hidden files on your computer.Anti Tracks support Internet Explorer, AOL, Netscape/Mozilla and Opera browsers. It also include more than 85 free plug-ins to extend erasing features to support popular programs such as ACDSee, Acrobat Reader, KaZaA, PowerDVD, WinZip, iMesh, Winamp and much more. Also you can easily schedule erasing tasks at specific time intervals or at Windows stat-up/ shutdown.To ensure maximum privacy protection Anti Tracks implements the US Department of Defense DOD 5220.22-M, Gutmann and NSA secure erasing methods, making any erased files unrecoverable even when using advanced recovery tools.

East-Tec Eraser

East-Tec Eraser goes beyond U.S. Department of Defense standards for the permanent erasure of digital information and easily removes every trace of sensitive data from your computer.

Completely destroy information stored without your knowledge or approval: Internet history, Web pages and pictures from sites visited on the Internet, unwanted cookies, chatroom conversations, deleted e-mail messages, temporary files, the Windows swap file, the Recycle Bin, previously deleted files, valuable corporate trade secrets, Business plans, personal files, photos or confidential letters, etc.East-Tec Eraser 2005 offers full support for popular browsers (Internet Explorer, Netscape Navigator, America Online, MSN Explorer, Opera), for Peer2Peer applications (Kazaa, Kazaa Lite, iMesh, Napster, Morpheus, Direct Connect, Limewire, Shareaza, etc.), and for other popular programs such as Windows Media Player, RealPlayer, Yahoo Messenger, ICQ, etc. Eraser has an intuitive interface and wizards that guide you through all the necessary steps needed to protect your privacy and sensitive information.Other features include support for custom privacy needs, user-defined erasure methods, command-line parameters, integration with Windows Explorer, and password protection.

Ghostsurf Platinum

GhostSurf Platinum ensures your safety online by providing an anonymous, encrypted Internet connection, and GhostSurf stops spyware, eliminates ads and erases your tracks. GhostSurf lets you customize your privacy level in real-time to suit your surfing needs. A variety of options enable you to block personal information, mask your IP address, route your data through anonymous hubs and even encrypt your Internet connection. GhostSurf's Privacy Control Center allows you to see and block every piece of data that your computer emits over the Internet, preventing even your Internet Service Provider (ISP) from creating a profile on you.

生成出来的图片就大概是这样子的:
词云


带有轮廓的文字云

这里需要多几步,我们需要准备一张图片,注意,需要有黑色封闭轮廓且背景需要是白色,例如下图:
示例图片
在准备我们的txt文件,这一次,我们就需要宣传标语:

红色,中华,精神
爱国之心,国之命脉。
奋不顾身
传承红色圣火,共建绿色家园,
发扬革命传统,争取更大光荣,
畅游红色故土,饱览大好河山,
缅怀革命先烈,弘扬爱国精神,
畅游红色经典,发扬红色精神,
玩转五仙山,好运违你转,
红色体验经典,健康成长乐园,
染红快乐,畅享精彩,
红色乐园,绿色氧吧,金色童年,
红色乐园,金色年华,
感受红色文化,体验现时乐趣,
自古英雄出少年,少年乐园红色先,
红旗飘飘,我心飞扬,
红色乐园,赤子之行,
亲身重走长征路,身心历练不畏难,
领略红色文化,畅游激情海洋,
追寻红色记忆,体味幸福生活,

接下来的代码中,我们需要加上获取轮廓的一段:

import wordcloud
import numpy as np
from PIL import Image  # Image模块是在Python PIL图像处理常用的模块
import jieba
import matplotlib.pyplot as plt


def word_cloud_shape():
    pic = Image.open("图片路径")  # 打开图片路径,形成轮廓
    shape = np.array(pic)  # 图像轮廓转换为数组
    wc = wordcloud.WordCloud(mask=shape, font_path="simkai.ttf", background_color="white",
                             max_font_size=100)  # mask为图片背景,font_path为字体,若不设置可能乱码

    text = open(r'文本路径', "r", encoding='gb2312').read()  # 对中文应该设置编码方式为gb2312
    cut_text = jieba.cut(text)
    result = " ".join(cut_text)
    wc.generate(result)
    wc.to_file("图片路径")


word_cloud_shape()

生成出来的图片:
效果图


结语

在文章的最后,希望那些像我一样的少先队员,共青团员能积极投入到红色教育的宣传当中,也祝愿伟大的祖国母亲能越来越富强,越来越振兴!!!
今天的分享就到这里了,886!

猜你喜欢

转载自blog.csdn.net/m0_70457107/article/details/128811277