Commit 0808721e authored by Andreas Mueller's avatar Andreas Mueller

some docs, add to __init__

parent b8039845
......@@ -9,8 +9,7 @@ from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
from wordcloud.color_from_image import ImageColorGenerator
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
d = path.dirname(__file__)
......@@ -18,6 +17,7 @@ d = path.dirname(__file__)
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
alice_coloring = imread(path.join(d, "alice_color.png"))
wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
......
......@@ -114,14 +114,14 @@ def test_mask():
# check that using an empty mask is equivalent to not using a mask
wc = WordCloud(random_state=42)
wc.generate(THIS)
mask = np.zeros(np.array(wc).shape[:2])
mask = np.zeros(np.array(wc).shape[:2], dtype=np.int)
wc_mask = WordCloud(mask=mask, random_state=42)
wc_mask.generate(THIS)
assert_array_equal(wc, wc_mask)
# use actual nonzero mask
mask = np.zeros((234, 456))
mask[100:150, 300:400] = 1
mask = np.zeros((234, 456), dtype=np.int)
mask[100:150, 300:400] = 255
wc = WordCloud(mask=mask)
wc.generate(THIS)
......
from .wordcloud import WordCloud, STOPWORDS, random_color_func
from .color_from_image import ImageColorGenerator
__all__ = ['WordCloud', 'STOPWORDS', 'random_color_func']
__all__ = ['WordCloud', 'STOPWORDS', 'random_color_func', 'ImageColorGenerator']
......@@ -3,6 +3,20 @@ from PIL import ImageFont
class ImageColorGenerator(object):
"""Color generator based on a color image.
Generates colors based on an RGB image. A word will be colored using
the mean color of the enclosing rectangle in the color image.
After construction, the object acts as a callable that can be passed as
color_func to the word cloud constructor or to the recolor method.
Parameters
----------
image : nd-array, shape (height, width, 3)
Image to use to generate word colors. Alpha channels are ignored.
This should be the same size as the canvas. for the wordcloud.
"""
# returns the average color of the image in that region
def __init__(self, image):
if image.ndim not in [2, 3]:
......@@ -14,6 +28,7 @@ class ImageColorGenerator(object):
self.image = image
def __call__(self, word, font_size, font_path, position, orientation, **kwargs):
"""Generate a color for a given word using a fixed image."""
# get the font to get the box size
font = ImageFont.truetype(font_path, font_size)
transposed_font = ImageFont.TransposedFont(font,
......
......@@ -182,7 +182,7 @@ class WordCloud(object):
height = mask.shape[0]
if mask.dtype.kind == 'f':
warnings.warn("mask image should be unsigned byte between 0 and"
"255. Got a float array")
" 255. Got a float array")
if mask.ndim == 2:
boolean_mask = mask == 255
elif mask.ndim == 3:
......
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