Commit c8194240 authored by Andreas Mueller's avatar Andreas Mueller Committed by GitHub

Merge pull request #233 from amueller/imshow_interpolation

Imshow interpolation
parents 5bd14a23 16b1f721
......@@ -14,7 +14,8 @@ import random
from wordcloud import WordCloud, STOPWORDS
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
def grey_color_func(word, font_size, position, orientation, random_state=None,
**kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
d = path.dirname(__file__)
......@@ -43,11 +44,12 @@ wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,
# store default colored image
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3))
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
interpolation="bilinear")
wc.to_file("a_new_hope.png")
plt.axis("off")
plt.figure()
plt.title("Default colors")
plt.imshow(default_colors)
plt.imshow(default_colors, interpolation="bilinear")
plt.axis("off")
plt.show()
......@@ -2,12 +2,13 @@
"""
Image-colored wordcloud
========================
You can color a word-cloud by using an image-based coloring strategy implemented in
ImageColorGenerator. It uses the average color of the region occupied by the word
in a source image. You can combine this with masking - pure-white will be interpreted
as 'don't occupy' by the WordCloud object when passed as mask.
If you want white as a legal color, you can just pass a different image to "mask",
but make sure the image shapes line up.
You can color a word-cloud by using an image-based coloring strategy
implemented in ImageColorGenerator. It uses the average color of the region
occupied by the word in a source image. You can combine this with masking -
pure-white will be interpreted as 'don't occupy' by the WordCloud object when
passed as mask.
If you want white as a legal color, you can just pass a different image to
"mask", but make sure the image shapes line up.
"""
from os import path
......@@ -22,8 +23,8 @@ d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()
# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
# read the mask / color image taken from
# http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
alice_coloring = np.array(Image.open(path.join(d, "alice_color.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
......@@ -37,14 +38,14 @@ wc.generate(text)
image_colors = ImageColorGenerator(alice_coloring)
# show
plt.imshow(wc)
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.figure()
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
plt.imshow(wc.recolor(color_func=image_colors))
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray)
plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")
plt.axis("off")
plt.show()
......@@ -52,17 +52,18 @@ class GroupedColorFunc(object):
"""
def __init__(self, color_to_words, default_color):
self.color_func_to_words = [(get_single_color_func(color), set(words))
for (color, words) in color_to_words.items()]
self.color_func_to_words = [
(get_single_color_func(color), set(words))
for (color, words) in color_to_words.items()]
self.default_color_func = get_single_color_func(default_color)
def get_color_func(self, word):
"""Returns a single_color_func associated with the word"""
try:
color_func = next(color_func
for (color_func, words) in self.color_func_to_words
if word in words)
color_func = next(
color_func for (color_func, words) in self.color_func_to_words
if word in words)
except StopIteration:
color_func = self.default_color_func
......@@ -122,6 +123,6 @@ wc.recolor(color_func=grouped_color_func)
# Plot
plt.figure()
plt.imshow(wc)
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
......@@ -34,9 +34,9 @@ wc.generate(text)
wc.to_file(path.join(d, "alice.png"))
# show
plt.imshow(wc)
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray)
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()
......@@ -19,7 +19,7 @@ wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
......@@ -30,5 +30,5 @@ plt.axis("off")
plt.show()
# The pil way (if you don't have matplotlib)
#image = wordcloud.to_image()
#image.show()
# image = wordcloud.to_image()
# image.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment