Categories
Uncategorized

Python WordCloud image

wordcloud created by Python

If you pay attention to my main page, you will see that I have replaced my cover / featured picture. This picture was created by using Python to read through all my posts and found all the common words appeared in my posts. I will post the script that I used to pull all my posts from wordpress next time. The image is based on this picture “cover.png”

To begin, you will need to install some of the modules first. You can do so by using the pip install [module name] statement in your python interpreter. I am using Visual Studio Code.

#import modules

import numpy
from os import path
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
from PIL import Image

#open cover.png
shape = numpy.array(Image.open("cover.png"))

#define which words to exclude
stopwords= set(STOPWORDS)
stopwords.update(["nbsp","will","need","one","using","name","use","file","right","click","new","now"])

dirname = path.dirname(__file__)

#open wordcloud.txt that contains all my posts

text = open(path.join(dirname, 'wordcloud.txt'),'r',encoding='utf-8').read()

#Define parameter for the wordCloud image

wordcloud = WordCloud(stopwords=stopwords, max_words=500, background_color="white", contour_width=5, mask=shape, contour_color='black').generate(text)

plt.imshow(wordcloud, interpolation="bilinear")

plt.axis("off")

plt.show()



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s