python编程图_Python编程实战:老司机奇技淫巧系列之字符转换成图片

本文是一篇Python编程教程,介绍了如何利用wordcloud模块创建词云图。通过安装numpy, scipy, jieba等依赖库,结合jieba进行中文分词,最后展示了一段代码示例,生成了基于背景图片的彩色词云效果。同时,文章提供了源码链接供读者参考。
摘要由CSDN通过智能技术生成

本篇Pyhton编程实践教程将为大家讲解Pyhton编程的知识点,看完这篇文章会让你对Python编程的知识点有更加清晰的理解和运用。

先放效果图:

1520304793413005746.png

还有这个:

1520304821369028111.png

1520304852696067263.png

是不是立马逼格满满~

这里用到的是一个有趣的模块,叫wordcloud:

github: https://github.com/amueller/word_cloud

官网: https://amueller.github.io/word_cloud/

*建议自行通过下载setup.py的方式安装,pip install 不一定能下载成功。

打开,并下载: https://github.com/amueller/word_cloud/archive/master.zip

然后 python setup.py install

安装其它依赖的模块:

必须安装第一步安装numpy: https://pypi.python.org/pypi/numpy

scipy: https://pypi.python.org/pypi/scipy

jieba: https://pypi.python.org/pypi/jieba/

下载whl文件,然后 pip install XXXX.whl

如果出现错误,请参考https://www.cnblogs.com/nice-forever/p/5371906.html

分享一下源码:

#coding:utf-8

#author http://blog.csdn.net/fyuanfena/article/details/52038984

from os import pathfrom scipy.misc import imreadimport matplotlib.pyplot as pltimport jieba

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

stopwords = {}def importStopword(filename=''):

global stopwords

f = open(filename, 'r', encoding='utf-8')

line = f.readline().rstrip()

while line:

stopwords.setdefault(line, 0)

stopwords[line] = 1

line = f.readline().rstrip()

f.close()

def processChinese(text):

seg_generator = jieba.cut(text) # 使用jieba分词,也可以不使用

seg_list = [i for i in seg_generator if i not in stopwords]

seg_list = [i for i in seg_list if i != u' ']

seg_list = r' '.join(seg_list)

return seg_list

importStopword(filename='./stopwords.txt')

# 获取当前文件路径

# __file__ 为当前文件, 在ide中运行此行会报错,可改为

# d = path.dirname('.')

d = path.dirname(__file__)

text = open(path.join(d, u'love.txt'),encoding ='utf-8').read()

#如果是中文

text = processChinese(text)#中文不好分词,使用Jieba分词进行

# read the mask / color image

# 设置背景图片

back_coloring = imread(path.join(d, "./image/love.jpg"))

wc = WordCloud( font_path='./font/cabin-sketch.bold.ttf ', #设置字体 要是使用汉字就用simhei.ttf

background_color="white", #背景颜色

max_words=1000,# 词云显示的最大词数

mask=back_coloring,#设置背景图片

max_font_size=80, #字体最大值

random_state=10, #42 )# 生成词云, 可以用generate输入全部文本(中文不好分词),也可以我们计算好词频后使用generate_from_frequencies函数wc.generate(text)# wc.generate_from_frequencies(txt_freq)

# txt_freq例子为[('词a', 100),('词b', 90),('词c', 80)]

# 从背景图片生成颜色值

image_colors = ImageColorGenerator(back_coloring)

plt.figure()# 以下代码显示图片plt.imshow(wc)

plt.axis("off")

plt.show()# 绘制词云

# 保存图片

wc.to_file(path.join(d, "名1称.png"))

官方的samplecode给出的效果图示例:

#!/usr/bin/env python"""

Image-colored wordcloud

=======================

You can color a word-cloud by using an image-based coloring strategy

implemented in ImageColorGenerator. It uses the average color of the region

occupied by the word in a source image. You can combine this with masking -

pure-white will be interpreted as 'don't occupy' by the WordCloud object when

passed as mask.

If you want white as a legal color, you can just pass a different image to

"mask", but make sure the image shapes line up."""

from os import pathfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

d = path.dirname(__file__)

# Read the whole text.

text = open(path.join(d, 'alice.txt')).read()

# read the mask / color image taken from

# http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010

alice_coloring = np.array(Image.open(path.join(d, "alice_color.png")))

stopwords = set(STOPWORDS)

stopwords.add("said")

wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,

stopwords=stopwords, max_font_size=40, random_state=42)# generate word cloudwc.generate(text)

# create coloring from image

image_colors = ImageColorGenerator(alice_coloring)

# show

plt.imshow(wc, interpolation="bilinear")

plt.axis("off")

plt.figure()# recolor wordcloud and show

# we could also give color_func=image_colors directly in the constructor

plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")

plt.axis("off")

plt.figure()

plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")

plt.axis("off")

plt.show()

1520304893028091262.png

1520304906416096973.png

最后感谢 http://blog.csdn.net/fyuanfena/article/details/52038984

和ta的项目源码:https://github.com/fyuanfen/wordcloud

以上,关于Pyhton的全部内容讲解完毕啦,欢迎大家继续关注!更多关于Python的干货请关注职坐标Python频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值