def set_wordcloud_image(words):
if words:
# WordCloud Option
wc = WordCloud(
background_color=settings.WORDCLOUD_BACKGROUND_COLOR,
width=settings.WORDCLOUD_WIDTH,
height=settings.WORDCLOUD_HEIGHT,
max_words=settings.WORDCLOUD_MAX_WORDS,
max_font_size=settings.WORDCLOUD_MAX_FRONT_SIZE,
scale=settings.WORDCLOUD_SCALE,
)
wordcloud_img = wc.generate(words).to_image()
return wordcloud_img
return None
python类WordCloud()的实例源码
def word_cloud(f):
wordcloud = WordCloud().generate_from_frequencies(f)
# Open a plot of the generated image.
plt.imshow(wordcloud)
plt.axis("off")
plt.savefig('out/word_cloud.png', dpi=300, format='png')
def generate_wordcloud():
from wordcloud import WordCloud
wordcloud = WordCloud(background_color="white")
from operator import itemgetter
item1 = itemgetter(1)
frequencies = sorted(label_counts.items(), key=item1, reverse=True)
wordcloud.generate_from_frequencies(frequencies)
# save image
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.savefig('photo_tags')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--url', metavar='URL', default=None, help='input the url')
parser.add_argument('--output', metavar='OUTPUT', default='./wordcloud.jpg', help='input the output_file')
parser.add_argument('--input', metavar='INPUT_FIEL', default=None, help='input the input_file')
parser.add_argument('--model', metavar='INPUT_IMAGE_MODEL', default=None, help='input the input_image_model')
parser.add_argument('--ttf', metavar='INPUT_TTF', default='./font/simhei.ttf', help='input the typeface')
parser.add_argument('--width', metavar='INPUT_WIDTH', default=1800, type=int, help='input the image width')
parser.add_argument('--height', metavar='INPUT_HEIGHT', default=1000, type=int, help='input the image height')
parser.add_argument('--bg', metavar='INPUT_BACKGROUND_COLOR', default='black', help='input the image background_color')
parser.add_argument('--margin', metavar='INPUT_MARGIN', default=5, type=int, help='input the image margin')
parser.add_argument('--max_font_size', metavar='INPUT_max_font_size', default=60, type=int, help='input the max_font_size')
args = parser.parse_args()
url = args.url
output_file = args.output
input_file = args.input
model_path = args.model
typeface = args.ttf
max_font_size=args.max_font_size
width = args.width
height = args.height
background_color = args.bg
margin = args.margin
try:
image_mask = np.array(PIL.Image.open(model_path))
except:
image_mask=None
wordcloud = WordCloud(font_path=typeface, mask=image_mask, max_font_size=max_font_size,
background_color=background_color, margin=margin, width=width, height=height)
try:
txt_join = get_txt(input_file)
wordcloud_ = wordcloud.generate(txt_join)
except:
html_text = get_html_text(url)
wordcloud_ = wordcloud.generate(html_text)
image = wordcloud_.to_image()
image.save(output_file)
def show(self):
wordcloud = WordCloud(
font_path=u'./static/simheittf/simhei.ttf',
background_color="black", max_words=40, margin=5, width=1000, height=800)
wordcloud = wordcloud.generate(self.seg_text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
def save_wordcloud_image(frequencies, filename):
wordcloud = WordCloud(width=1024, height=786, min_font_size=1).fit_words(frequencies)
fig = plt.figure()
fig.set_figwidth(12)
fig.set_figheight(16)
plt.imshow(wordcloud)
plt.axis("off")
plt.savefig(filename, facecolor='k', bbox_inches='tight')
print('imaged created')
def generate_image(words, image):
graph = np.array(image)
wc = WordCloud(font_path=os.path.join(CUR_DIR, 'fonts/simhei.ttf'),
background_color='white', max_words=MAX_WORDS, mask=graph)
wc.generate_from_frequencies(words)
image_color = ImageColorGenerator(graph)
return wc, image_color
def wordcloudplot(txt,name):
path = 'msyh.ttf'
path = unicode(path, 'utf8').encode('gb18030')
alice_mask = np.array(PIL.Image.open('jay.jpg'))
wordcloud = WordCloud(font_path=path,
background_color="white",
margin=5, width=1800, height=800, mask=alice_mask, max_words=2000, max_font_size=60,
random_state=42)
wordcloud = wordcloud.generate(txt)
wordcloud.to_file('../songs/'+name+'/'+name+'.jpg')
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
def get_wordcloud(file_path):
with open(file_path, 'r') as f:
text = f.read()
wordcloud = WordCloud(max_font_size=200, min_font_size=25, prefer_horizontal=1, background_color='white', margin=0,
relative_scaling=0.5, colormap='copper', collocations=False, width=1600, height=800).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
def createImage(self, arr, saveName):
text = " ".join(arr)
savedir = path.join(self.d,self.e, saveName) # local image gets overwritten each time. will this break if too many requests?
wc = WordCloud(max_words=20000, stopwords=self.STOPWORDS).generate(text)
wc.to_file(savedir)
return savedir
# idk how it subscribes to the event... but it works!
def generate(self, title, text):
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# plt.show()
filename = title + '.png'
plt.savefig(filename, bbox_inches='tight')
def draw_wordcloud(file_name):
with codecs.open(file_name,encoding='utf-8') as f:
comment_text=f.read()
color_mask=imread('template.png') #??????
stopwords = ['png','douban','com','href','https','img','img3','class','source','icon','shire',u'??',u'??',u'??',u'??',u'??',u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??',
u'??', u'??', u'??', u'??']
font = r'C:\Windows\Fonts\simfang.ttf'
cloud=WordCloud(font_path=font,background_color='white',max_words=20000,max_font_size=200,min_font_size=4,mask=color_mask,stopwords=stopwords)
word_cloud=cloud.generate(comment_text) #????
word_cloud.to_file('pjl_cloud.jpg')
def draw_wordcloud(file_name):
with codecs.open(file_name,encoding='utf-8') as f:
comment_text=f.read()
color_mask=imread('template.png') #??????
stopwords = [u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??', u'??',
u'??', u'??', u'??', u'??']
font = r'C:\Windows\Fonts\simfang.ttf'
cloud=WordCloud(font_path=font,background_color='white',max_words=20000,max_font_size=200,min_font_size=4,mask=color_mask,stopwords=stopwords)
word_cloud=cloud.generate(comment_text) #????
word_cloud.to_file('pjl_cloud.jpg')
def show(self):
# wordcloud = WordCloud(max_font_size=40, relative_scaling=.5)
wordcloud = WordCloud(font_path=u'./static/simheittf/simhei.ttf',
background_color="black", margin=5, width=1800, height=800)
wordcloud = wordcloud.generate(self.seg_text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
def get_wordclud(file_set):
line_set = []
for j in range(10):
reader=csv.reader(open(file_set[j], 'r'))
for line in reader:
line_set.append(line[1])
word_list = [" ".join(jieba.cut(sentence)) for sentence in line_set]
new_text = ' '.join(word_list)
wordcloud = WordCloud(font_path="C:/Python34/Lib/site-packages/wordcloud/simhei.ttf", background_color="black").generate(new_text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
def gen_wordcloud():
then = common_functions.queryrange(1)
body = '''{
"size" : 10000,
"query": {
"constant_score": {
"filter": {
"range": {
"epoch": {
"from": '''+then+'''
}
}
}
}
}
}'''
text = common_functions.pull_mailtext_24hrs(es, es_collection_name, body, keywords_list).lower()
print text
print
wc = WordCloud(background_color="white", max_words=40)
fileloc = "/home/pierre/es_email_intel/wordcloud.png"
try:
wc.generate(text)
wc.to_file(fileloc)
print 'Finished!'
return
except:
target = open(fileloc, 'w')
target.truncate()
target.close()
print 'Except!'
return
def drawPic(text,Pic):
#img=imread(Pic,flatten=True)
w=WordCloud(font_path="C:/Windows/Fonts/simhei.ttf",background_color='white').generate(text)
plt.imshow(w)
plt.axis("off")
plt.savefig("F:/EduSpider/edubug.jpg",dpi=600)
def get_word_to_cloud(self):
for file in self.file_list:
with codecs.open('../spider/' + file, "r",encoding='utf-8', errors='ignore') as string:
#??????????????????
string = string.read().upper()
#???????????
res = jieba.cut(string, HMM=False)
reslist = list(res)
wordDict = {}
#???????????
for i in reslist:
if i not in self.dic_list:
continue
if i in wordDict:
wordDict[i]=wordDict[i]+1
else:
wordDict[i] = 1
#???????
coloring = imread('test.jpeg')
#???????????????
wc = WordCloud(font_path='msyh.ttc',mask=coloring,
background_color="white", max_words=50,
max_font_size=40, random_state=42)
wc.generate_from_frequencies(wordDict)
#????
wc.to_file("%s.png"%(file))
#???????
def __init__(self, fontPath):
self.client = MongoClient()
self.coll = self.client[dbName][collName]
self.fontPath = fontPath
self.wordCloud = WordCloud(font_path=self.fontPath, width=400, height=400, max_words=100)
if not os.path.exists(self.imgDir):
os.mkdir(self.imgDir)
logging.info('GroupTagCloud connected to MongoDB.')
def generate_img(data):
mask_img = imread('./heart-mask.jpg')
wordcloud = WordCloud(
font_path='/Library/Fonts/Songti.ttc',
background_color='white',
mask=mask_img
).generate(data)
plt.imshow(wordcloud)
plt.axis('off')
# plt.show()
plt.savefig('./heart.jpg', dpi=600)