Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug for drawing mask contour on a transparent background. #389

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/test_wordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ def test_mask_contour():
assert_true(all(sm_array[100, 300] == [0, 0, 255]))


def test_mask_contour_transparent():
# test if mask contour can be drawn on a transparent background
# https://github.com/amueller/word_cloud/pull/389
mask = np.ones((600, 600, 3), dtype='ubyte')
mask[:, -10:] = 0
w = WordCloud(mode='RGBA', background_color=None, contour_color='blue', contour_width=2, mask=mask,
max_words=50, width=800, height=800)
w.generate(THIS)
img = w.to_image()


def test_single_color_func():
# test single color function for different color formats
random = Random(42)
Expand Down
14 changes: 8 additions & 6 deletions wordcloud/wordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def __init__(self, font_path=None, width=400, height=200, margin=2,
max_font_size=None, font_step=1, mode="RGB",
relative_scaling=.5, regexp=None, collocations=True,
colormap=None, normalize_plurals=True, contour_width=0,
contour_color='black'):
contour_color=None):
if font_path is None:
font_path = FONT_PATH
if color_func is None and colormap is None:
Expand Down Expand Up @@ -697,7 +697,7 @@ def _get_bolean_mask(self, mask):

def _draw_contour(self, img):
"""Draw mask contour on a pillow image."""
if self.mask is None or self.contour_width == 0:
if self.mask is None or self.contour_color is None or self.contour_width == 0:
return img

mask = self._get_bolean_mask(self.mask) * 255
Expand All @@ -715,12 +715,14 @@ def _draw_contour(self, img):
contour = Image.fromarray(contour)
contour = contour.filter(ImageFilter.GaussianBlur(radius=radius))
contour = np.array(contour) > 0
contour = np.dstack((contour, contour, contour))
if img.mode == 'RGBA':
contour = np.dstack((contour, contour, contour, contour))
else:
contour = np.dstack((contour, contour, contour))

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

return Image.fromarray(ret)
2 changes: 1 addition & 1 deletion wordcloud/wordcloud_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def parse_args(arguments):
dest='contour_width',
help='if greater than 0, draw mask contour (default: 0)')
parser.add_argument(
'--contour_color', metavar='color', default='black', type=str,
'--contour_color', metavar='color', default=None, type=str,
dest='contour_color',
help='use given color as mask contour color -'
' accepts any value from PIL.ImageColor.getcolor')
Expand Down