我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用wordcloud.ImageColorGenerator()。
def showData(self): print('???,????···') mask = imread(self.picfile) imgcolor = ImageColorGenerator(mask) wcc = WordCloud(font_path='./msyhl.ttc', mask=mask, background_color='white', max_font_size=200, max_words=300, color_func=imgcolor ) wc = wcc.generate_from_frequencies(self.data) plt.figure() plt.imshow(wc) plt.axis('off') print('?????') plt.show()
def create_cloud(self): # Return Bing search snippets text = self.return_txt() # Get mask image from Bing image_mask = np.array(self.return_img()) # potential feature stopwords = set(STOPWORDS) # stopwords.add(search_modifier) wordcloud = WordCloud(background_color="white", mask=image_mask, stopwords=stopwords) wordcloud.generate(text) image_colors = ImageColorGenerator(image_mask) plt.imshow(image_mask, cmap=plt.cm.gray, interpolation="None") plt.imshow(wordcloud.recolor(color_func=image_colors), alpha=.8, interpolation='None') plt.axis("off") return plt
def generateWordCloud(text): # read the mask / color image # taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010 d = path.dirname(__file__) cloud_coloring = np.array(Image.open(path.join(d, "us-mask-white.png"))) stopwords = set(STOPWORDS) stopwords.add("said") wc = WordCloud(background_color="black", max_words=2000, mask=cloud_coloring, stopwords=stopwords, max_font_size=40, random_state=42) # generate word cloud wc.generate(text) # create coloring from image image_colors = ImageColorGenerator(cloud_coloring) # show plt.imshow(wc) plt.axis("off") plt.show()
def drawWordCloud(word_text, filename): mask = imread('hello.jpg') my_wordcloud = WordCloud( background_color='white', # ?????? mask=mask, # ?????? max_words=2000, # ????????? stopwords=STOPWORDS, # ????? font_path='/System/Library/Fonts/Hiragino Sans GB W6.ttc', # ????????????????? max_font_size=50, # ??????? random_state=30, # ?????????????????????? scale=1 ).generate(word_text) image_colors = ImageColorGenerator(mask) my_wordcloud.recolor(color_func=image_colors) # ???????? plt.imshow(my_wordcloud) plt.axis("off") plt.show() # ???? my_wordcloud.to_file(filename=filename) print()
def drawWordCloud(word_text, filename): mask = imread('bike.jpg') my_wordcloud = WordCloud( background_color='white', # ?????? mask=mask, # ?????? max_words=2000, # ????????? stopwords=STOPWORDS, # ????? font_path='/System/Library/Fonts/Hiragino Sans GB W6.ttc', # ????????????????? max_font_size=50, # ??????? random_state=30, # ?????????????????????? scale=1.3 ).generate(word_text) image_colors = ImageColorGenerator(mask) my_wordcloud.recolor(color_func=image_colors) # ???????? plt.imshow(my_wordcloud) plt.axis("off") plt.show() # ???? my_wordcloud.to_file(filename=filename) print()
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 main(input_filename): content = '\n'.join([line.strip() for line in codecs.open(input_filename, 'r', 'utf-8') if len(line.strip()) > 0]) stopwords = set([line.strip() for line in codecs.open(stopwords_filename, 'r', 'utf-8')]) segs = jieba.cut(content) words = [] for seg in segs: word = seg.strip().lower() if len(word) > 1 and word not in stopwords: words.append(word) words_df = pandas.DataFrame({'word':words}) words_stat = words_df.groupby(by=['word'])['word'].agg({'number' : np.size}) words_stat = words_stat.reset_index().sort_values(by="number",ascending=False) print '# of different words =', len(words_stat) input_prefix = input_filename if input_filename.find('.') != -1: input_prefix = '.'.join(input_filename.split('.')[:-1]) for file in listdir(template_dir): if file[-4:] != '.png' and file[-4:] != '.jpg': continue background_picture_filename = join(template_dir, file) if isfile(background_picture_filename): prefix = file.split('.')[0] bimg=imread(background_picture_filename) wordcloud=WordCloud(font_path=font_filename,background_color='white',mask = bimg,max_font_size=600,random_state=100) wordcloud=wordcloud.fit_words(dict(words_stat.head(4000).itertuples(index=False))) bimgColors=ImageColorGenerator(bimg) wordcloud.recolor(color_func=bimgColors) output_filename = prefix + '_' + input_prefix + '.png' print 'Saving', output_filename wordcloud.to_file(output_filename)