Skip to content

Commit 26dfd5e

Browse files
committed
Fixed bug in mask mode when drawing contour on a transparent background.
The contour used was lacking a transparency channel. Also fixed the conditions for plotting a contour. Now contour_color=None or contour_width=0 means that no contour will be plotted.
1 parent c6a5853 commit 26dfd5e

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

wordcloud/wordcloud.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def __init__(self, font_path=None, width=400, height=200, margin=2,
278278
max_font_size=None, font_step=1, mode="RGB",
279279
relative_scaling=.5, regexp=None, collocations=True,
280280
colormap=None, normalize_plurals=True, contour_width=0,
281-
contour_color='black'):
281+
contour_color=None):
282282
if font_path is None:
283283
font_path = FONT_PATH
284284
if color_func is None and colormap is None:
@@ -697,7 +697,7 @@ def _get_bolean_mask(self, mask):
697697

698698
def _draw_contour(self, img):
699699
"""Draw mask contour on a pillow image."""
700-
if self.mask is None or self.contour_width == 0:
700+
if self.mask is None or self.contour_color is None or self.contour_width == 0:
701701
return img
702702

703703
mask = self._get_bolean_mask(self.mask) * 255
@@ -715,12 +715,14 @@ def _draw_contour(self, img):
715715
contour = Image.fromarray(contour)
716716
contour = contour.filter(ImageFilter.GaussianBlur(radius=radius))
717717
contour = np.array(contour) > 0
718-
contour = np.dstack((contour, contour, contour))
718+
if img.mode == 'RGBA':
719+
contour = np.dstack((contour, contour, contour, contour))
720+
else:
721+
contour = np.dstack((contour, contour, contour))
719722

720723
# color the contour
721724
ret = np.array(img) * np.invert(contour)
722-
if self.contour_color != 'black':
723-
color = Image.new(img.mode, img.size, self.contour_color)
724-
ret += np.array(color) * contour
725+
color = np.array(Image.new(img.mode, img.size, self.contour_color))
726+
ret += color * contour
725727

726728
return Image.fromarray(ret)

wordcloud/wordcloud_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def parse_args(arguments):
118118
dest='contour_width',
119119
help='if greater than 0, draw mask contour (default: 0)')
120120
parser.add_argument(
121-
'--contour_color', metavar='color', default='black', type=str,
121+
'--contour_color', metavar='color', default=None, type=str,
122122
dest='contour_color',
123123
help='use given color as mask contour color -'
124124
' accepts any value from PIL.ImageColor.getcolor')

0 commit comments

Comments
 (0)