Skip to content

Instantly share code, notes, and snippets.

@kirshiyin89
Last active June 16, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirshiyin89/a09fe7b880a650301ac031e1ec9879da to your computer and use it in GitHub Desktop.
Save kirshiyin89/a09fe7b880a650301ac031e1ec9879da to your computer and use it in GitHub Desktop.
Create word clouds with shape
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import requests
import csv
file_ob = open(r"feedback.csv")
# read the feedback file
reader_ob = csv.reader(file_ob)
reader_contents = list(reader_ob)
words = ""
for row in reader_contents :
for feedback in row :
words = words + " " + feedback
# get the image from the internet
mask = np.array(Image.open(requests.get('https://image.shutterstock.com/image-vector/heart-vector-icon-600w-651132286.jpg', stream=True).raw))
# build the WordCloud image
wordcloud = WordCloud(width = 800, height = 400,mask=mask, background_color ='white', max_words = 20, random_state = 1).generate(words)
# save the image
wordcloud.to_file("wordcloud.png")
# show the WordCloud image
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.margins(x=0, y=0)
plt.tight_layout(pad = 0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment