python實戰===老司機奇技淫巧系列之字符轉換成圖片


先放兩張效果圖:

還有這個:

是不是立馬逼格滿滿~

這里用到的是一個有趣的模塊,叫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 path from scipy.misc import imread import matplotlib.pyplot as plt import 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 path
from PIL import Image
import numpy as np
import 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 cloud
wc.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()

 

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

和ta的項目源碼:https://github.com/fyuanfen/wordcloud

 

順便提一下


 

如果你也喜歡Python 這里有一群Python愛好者匯集在此。

關注微信公眾號:【軟件測試技術】,回復 888,獲取QQ群號。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM