def build_word_cloud(text,back_coloring_path=None,font_path=None,txt_freq=None,scale=5): """ text: 词云的内容 back_coloring_path: 使用背景图片 font_path: 使用的字体所在路径 txt_freq: 词云权重,覆盖词云的内容 scale: 图片清晰度 """ # 设置词云属性 wc = WordCloud( background_color="white", # 背景颜色 max_words=2000, # 词云显示的最大词数 max_font_size=100, # 字体最大值 random_state=42, scale=scale, width=1000, height=860, margin=2, # 设置图片默认的大小,但是如果使用背景图片的话,那么保存的图片大小将会按照其大小保存,margin为词语边缘距离 ) wc.font_path = font_path if font_path else 'SIMHEI.TTF' if back_coloring_path: back_coloring = imread(back_coloring_path) wc.mask = back_coloring if text: wc.generate(text) if txt_freq: wc.generate_from_frequencies(txt_freq) if back_coloring_path: image_colors = ImageColorGenerator(back_coloring) plt.figure() plt.imshow(wc) plt.axis("off") plt.show() # 绘制词云 wc.to_file('WordCloudDefautColors.png') if back_coloring_path: image_colors = ImageColorGenerator(back_coloring) plt.imshow(wc.recolor(color_func=image_colors)) else: plt.imshow(wc) plt.axis("off") # 绘制背景图片为颜色的图片 plt.figure() if back_coloring_path: plt.imshow(back_coloring, cmap=plt.cm.gray) else: plt.imshow(wc) plt.axis("off") plt.show() # 保存图片 wc.to_file('WordCloudColorsByImg.png')
print("请不要关闭页面,稍后会自动结束!!") #打开颜色文件 f = open('颜色选择.csv') yanse = f.read() f.close() result = yanse.split('\n', 164) countnumber = 0 for x in result: print(countnumber, ',164') countnumber = countnumber + 1 stopwords = stopword.split("\n") wc = WordCloud(prefer_horizontal=prefer_horizontal, scale=scale, width=width, height=height, stopwords=stopwords, font_path="font.ttf", background_color="white", repeat=True, relative_scaling=.5, max_words=max_words, max_font_size=300, min_font_size=10, colormap=x) if use_mask == 1: mask = np.array(Image.open("mask.png")) wc.mask = mask wc.generate(new_txt) name = x + ".png" wc.to_file(name)
def generate_wordcloud(text_path, mask_path=None, width=400, height=400, lan='en', font_path=None, want_worlds=[], stop_words=[], path_to_save='.'): """ generate a word cloud of the mask picture you provide and a word cloud color by your picture :param text_path: use to generate words :param mask_path: picture you want to show :param width: the width of the word cloud picture, if mask_path is not provided :param height: the height of the word cloud picture, if mask_path is not provided :param lan: the language of your text :param font_path: if lan is 'cn', a chinese font must provide :param want_worlds: the special word you don't want to separate :param stop_words: the words you don't want to show up in your word cloud :param path_to_save: the directory you want to save your word cloud (!! is directory not file) :return: no return """ image_colors = None wc = WordCloud(background_color='white', max_words=1000, max_font_size=400, random_state=42) # check path if not os.path.isfile(text_path): print('## text_path is invalid !!') return if lan == 'cn': # if the lan is cn, then the path of chinese font path can't be null if not os.path.isfile(font_path): print('## chinese font_path cannot be null !!') return text = _generate_cn_words(text=open(text_path).read(), want_words=want_worlds, stop_words=stop_words) wc.font_path = font_path else: text = open(text_path).read() if not os.path.isfile(font_path): wc.font_path = font_path if mask_path is None and width > 0 and height > 0: wc.height = height wc.width = width elif os.path.isfile(mask_path): mask = np.array(Image.open(mask_path)) wc.mask = mask image_colors = ImageColorGenerator(mask) else: print('## mask_path is invalid !!') return wc.generate(text=text) plt.imshow(wc.recolor(color_func=image_colors)) plt.axis('off') plt.show() if os.path.isdir(path_to_save): wc.to_file(os.path.join(path_to_save, 'words.png')) else: print('## path_to_save is invalid !!') return if mask_path is not None: img1 = Image.open(os.path.join(path_to_save, 'words.png')) img2 = Image.open(mask_path) width = img1.size[0] height = img1.size[1] for i in range(0, width): for j in range(0, height): data1 = (img1.getpixel((i, j))) data2 = (img2.getpixel((i, j))) if (data1[0] <= 250 or data1[1] <= 250 or data1[2] <= 250): img1.putpixel((i, j), (data2[0], data2[1], data2[2], 255)) # if (data1[0] == 255 # and data1[1] == 255 # and data1[2] == 255): # img1.putpixel((i, j), (205, 205, 205, 255)) plt.imshow(img1) plt.axis('off') plt.show() img1.save(os.path.join(path_to_save, 'wordcloud.png'))