python-更改png文件中单词的颜色(搜索单词并更改单词的颜色)

时间:2018-09-12 09:07:11

标签: python

我正在使用wordcloud生成PNG图像。我的文件包含超过220000个具有频率的条目,以生成wordcloud(因此很慢)。 我必须循环生成wordclouds并在文件中查找字符串,然后将该单词的颜色更改为红色(其余单词将显示为黑色)。

任何人都可以引导我找到一种在PNG图像中搜索特定单词并仅使用python更改PNG文件中该单词的颜色的方法吗?For instance, in the attached image would like to search for a word like game,and change its color to red

我已经尝试使用python中的wordcloud函数生成wordcloud,并使用以下函数更改颜色。使用此功能,我可以更改颜色,但是由于必须生成数百个图像,因此需要花费很多时间。 我一直在寻找一种生成图像的方法,然后在图像中搜索特定单词,然后使用python更改该单词的颜色。

<div class="line"></div>

<div class="pully">
  <img src="https://s8.postimg.cc/u6gyll9ud/p-pully-center.png" alt="">
</div>

<div class="pully_left"> <!-- for comparison -->
  <img src="https://s8.postimg.cc/u6gyll9ud/p-pully-center.png" alt="">
</div>

<!-- <img class="pully" src="https://s8.postimg.cc/vax8le4x1/p-pully.png" alt=""> -->

1 个答案:

答案 0 :(得分:0)

好吧,没关系,我也修改了解析器。 此方法有效,您将不得不使解析器重新适应文本的特定性质。

请阅读评论:我添加了一些关于良好编程实践的建议,您在编写其他人可以阅读的代码时可能要遵循的建议。

import matplotlib.pyplot as plt
# import numpy as np # Not used, do not import
# from PIL import Image # Not used, do not import
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
#to get rid of warning of open windows     # No, bad habit. Do not remove warnings until the moment you are ready
# plt.rcParams.update({'figure.max_open_warning': 0})    # to ship the final version of your code.
# import os.path # Not used, do not import

stopwords = set(STOPWORDS) # I think it also accepts stopwords as a list, check it
vocabulary = dict()
word_in_red = 'the'

def color_word(word, *args, **kwargs):
        if (word == word_in_red):
            color = '#ff0000' # red
        else:
            color = '#000000' # black
        return color

with open('f1.txt', encoding='utf-8') as file: # Do not use inf, it may be mistaken for numpy.inf
    for line in file.readlines(): # reads all lines one by one
        for word in line.split(' '): # parses each line, returns the words splits by blank spaces
            word_lower = word.lower() # lowers the case
            if word_lower not in vocabulary: # if word not in vocabulary, puts word in vocabulary with counter 1
                vocabulary[word_lower] = 1
            else: # else increases the counter by 1
                vocabulary[word_lower] += 1 

with open('f2.txt', encoding='utf-8') as file: # Same as above, don't use inf
    for line in file.readlines():
        for word in line.split(' '):
            word_lower = word.lower()
            if word_lower not in vocabulary:
                vocabulary[word_lower] = 1
            else:
                vocabulary[word_lower] += 1 

    wordcloud = WordCloud(stopwords=STOPWORDS,
                              background_color='white',
                              max_words=210000,
                              width=1500,
                              height=1000, color_func=color_word).generate_from_frequencies(vocabulary)                   

    plt.figure(figsize = (15, 15), facecolor = None)
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.tight_layout(pad = 0)
    plt.show()
    # plt.savefig(name+".eps",format='eps', dpi=1000) # Why is this here? I understand you want to save with
    # wordcloud
    wordcloud.to_file('wordcloud.png')
    print('Done')

输出:

WordCloud, 'the' in red