Skip to content

Commit

Permalink
improve contour test, make draw_contour private
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmedmar committed Mar 6, 2018
1 parent eaf3ef1 commit 605eb90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
19 changes: 9 additions & 10 deletions test/test_wordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,11 @@ def test_mask():


def test_mask_contour():
# test mask contour is created
# test mask contour is created, learn more at:
# https://github.com/amueller/word_cloud/pull/348#issuecomment-370883873
mask = np.zeros((234, 456), dtype=np.int)
mask[100:150, 300:400] = 255

bk = WordCloud(mask=mask, contour_width=1, contour_color='black')
bk.generate(THIS)
bk_array = np.array(bk)
bk_total = bk_array[100:150, 300:400].sum()

sm = WordCloud(mask=mask, contour_width=1, contour_color='blue')
sm.generate(THIS)
sm_array = np.array(sm)
Expand All @@ -223,12 +219,15 @@ def test_mask_contour():
sc_array = np.array(sc)
sc_total = sc_array[100:150, 300:400].sum()

# values were obtained experimentally
assert_equal(bk_total, 0)
assert_equal(sm_total, 75480)
assert_equal(lg_total, 422280)
# test `contour_width`
assert_greater(lg_total, sm_total)

# test contour varies with `scale`
assert_greater(sc_total, sm_total)

# test `contour_color`
assert_true(all(sm_array[100, 300] == [0, 0, 255]))


def test_single_color_func():
# test single color function for different color formats
Expand Down
20 changes: 9 additions & 11 deletions wordcloud/wordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,7 @@ def to_image(self):
int(position[0] * self.scale))
draw.text(pos, word, fill=color, font=transposed_font)

if self.mask is not None and self.contour_width > 0:
img = self.draw_contour(img=img, mask=self.mask,
contour_width=self.contour_width,
contour_color=self.contour_color)

return img
return self._draw_contour(img=img)

def recolor(self, random_state=None, color_func=None, colormap=None):
"""Recolor existing layout.
Expand Down Expand Up @@ -700,9 +695,12 @@ def _get_bolean_mask(self, mask):
% str(mask.shape))
return boolean_mask

def draw_contour(self, img, mask, contour_width=1, contour_color='black'):
def _draw_contour(self, img):
"""Draw mask contour on a pillow image."""
mask = self._get_bolean_mask(mask) * 255
if self.mask is None or self.contour_width == 0:
return img

mask = self._get_bolean_mask(self.mask) * 255
contour = Image.fromarray(mask.astype(np.uint8))
contour = contour.resize(img.size)
contour = contour.filter(ImageFilter.FIND_EDGES)
Expand All @@ -713,16 +711,16 @@ def draw_contour(self, img, mask, contour_width=1, contour_color='black'):
contour[:, [0, -1]] = 0

# use gaussian to change width, divide by 10 to give more resolution
radius = contour_width / 10
radius = self.contour_width / 10
contour = Image.fromarray(contour)
contour = contour.filter(ImageFilter.GaussianBlur(radius=radius))
contour = np.array(contour) > 0
contour = np.dstack((contour, contour, contour))

# color the contour
ret = np.array(img) * np.invert(contour)
if contour_color != 'black':
color = Image.new(img.mode, img.size, contour_color)
if self.contour_color != 'black':
color = Image.new(img.mode, img.size, self.contour_color)
ret += np.array(color) * contour

return Image.fromarray(ret)

0 comments on commit 605eb90

Please sign in to comment.