Membuat Word Cloud Artistik Menggunakan Python

Create Word Cloud into any Shape you want using Python

Wordcloud Sederhana Menggunakan Python

  • background_color = Warna latar belakang
  • max_words = Jumlah maksimal kata unik dalam satu dokumen
  • stopwords = Daftar stopwords
  • max_font_size = Ukuran huruf maksimal
  • random_state = Untuk memastikan bahwa angka acak dihasilkan dalam urutan yang sama, sehingga hasilnya akan sama meskipun dilakukan beberapa kali
  • width = ukuran lebar dari output
  • height = ukuran tinggi dari output

Web Scraping Berita dengan 4 Baris Kode menggunakan Python

from newspaper import Article
article = Article('https://en.wikipedia.org/wiki/Ice_cream')
article.download()
article.parse()
from wordcloud import STOPWORDS
wc = WordCloud(background_color="white", max_words=2000,
stopwords=STOPWORDS, max_font_size=256,
random_state=42, width=500, height=500)
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
Word cloud sederhana dengan parameter
font_path = 'path/to/font'
wc = WordCloud(stopwords=STOPWORDS, font_path=font_path,
background_color="white", max_words=2000,
max_font_size=256, random_state=42,
width=500, height=500)
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
Custom font
Mask
from PIL import Image
import numpy as np
mask = np.array(Image.open('path/to/image'))
wc = WordCloud(stopwords=STOPWORDS, font_path=font_path,
mask=mask, background_color="white",
max_words=2000, max_font_size=256,
random_state=42, width=mask.shape[1],
height=mask.shape[0])
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
Masked word cloud
def one_color_func(word=None, font_size=None, 
position=None, orientation=None,
font_path=None, random_state=None):
h = 160 # 0 - 360
s = 100 # 0 - 100
l = 50 # 0 - 100 return "hsl({}, {}%, {}%)".format(h, s, l)
wc = WordCloud(stopwords=STOPWORDS, font_path=font_path,
mask=mask, background_color="white",
max_words=2000, max_font_size=256,
random_state=42, width=mask.shape[1],
height=mask.shape[0], color_func=one_color_func)
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
One color
def similar_color_func(word=None, font_size=None,
position=None, orientation=None,
font_path=None, random_state=None):
h = 40 # 0 - 360
s = 100 # 0 - 100
l = random_state.randint(30, 70) # 0 - 100 return "hsl({}, {}%, {}%)".format(h, s, l)
wc = WordCloud(stopwords=STOPWORDS, font_path=font_path,
mask=mask, background_color="white",
max_words=2000, max_font_size=256,
random_state=42, width=mask.shape[1],
height=mask.shape[0], color_func=similar_color_func)
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
Similar colors
def multi_color_func(word=None, font_size=None,
position=None, orientation=None,
font_path=None, random_state=None):
colors = [[4, 77, 82],
[25, 74, 85],
[82, 43, 84],
[158, 48, 79]]
rand = random_state.randint(0, len(colors) - 1) return "hsl({}, {}%, {}%)".format(colors[rand][0], colors[rand][1], colors[rand][2])
wc = WordCloud(stopwords=STOPWORDS, font_path=font_path,
mask=mask, background_color="white",
max_words=2000, max_font_size=256,
random_state=42, width=mask.shape[1],
height=mask.shape[0], color_func=multi_color_func)
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
Multi colors
from wordcloud import ImageColorGenerator
mask_colors = ImageColorGenerator(mask)
wc = WordCloud(stopwords=STOPWORDS, font_path=font_path,
mask=mask, background_color="white",
max_words=2000, max_font_size=256,
random_state=42, width=mask.shape[1],
height=mask.shape[0], color_func=mask_colors)
wc.generate(article.text)
plt.imshow(wc, interpolation="bilinear")
plt.axis('off')
plt.show()
Generated Colors
Tetap Terhubung dengan Kami
Share this
×