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
Changes from 1 commit
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
Next Next commit
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.
m000 committed Jun 6, 2018
commit 26dfd5ed25d09124d3320a00b8cb0c861fb8f65d
14 changes: 8 additions & 6 deletions wordcloud/wordcloud.py
Original file line number Diff line number Diff line change
@@ -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:
@@ -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
@@ -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
@@ -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')