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

Can't display border if mode is RGBA and using mask #501

Open
rdelassus opened this issue Oct 23, 2019 · 7 comments
Open

Can't display border if mode is RGBA and using mask #501

rdelassus opened this issue Oct 23, 2019 · 7 comments
Labels

Comments

@rdelassus
Copy link

rdelassus commented Oct 23, 2019

Description

Can't display border if mode is RGBA and using a mask

Steps/Code to Reproduce

Example:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from wordcloud import WordCloud
a = 'this is a wordcloud show test'
mask_path = "my_mask.png"
zone_mask = np.array(Image.open(mask_path))
wc = WordCloud(mode="RGBA", background_color=(0,0,0,0), contour_width=1, contour_color="black", mask=zone_mask).generate(a)
wc.to_file('wc.png')
plt.imshow(wc)
plt.axis('off')
plt.savefig('plt.png')

Expected Results

word cloud in mask with transparent background and black contour

Actual Results

<ipython-input-67-67708b1987aa> in <module>
      7 zone_mask = np.array(Image.open(mask_path))
      8 wc = WordCloud(mode="RGBA", background_color=(0,0,0,0), contour_width=1, contour_color="black", mask=zone_mask).generate(a)
----> 9 wc.to_file('wc.png')
     10 plt.imshow(wc)
     11 plt.axis('off')

/opt/conda/envs/lucie/lib/python3.7/site-packages/wordcloud/wordcloud.py in to_file(self, filename)
    689         """
    690 
--> 691         img = self.to_image()
    692         img.save(filename, optimize=True)
    693         return self

/opt/conda/envs/lucie/lib/python3.7/site-packages/wordcloud/wordcloud.py in to_image(self)
    632             draw.text(pos, word, fill=color, font=transposed_font)
    633 
--> 634         return self._draw_contour(img=img)
    635 
    636     def recolor(self, random_state=None, color_func=None, colormap=None):

/opt/conda/envs/lucie/lib/python3.7/site-packages/wordcloud/wordcloud.py in _draw_contour(self, img)
    753 
    754         # color the contour
--> 755         ret = np.array(img) * np.invert(contour)
    756         if self.contour_color != 'black':
    757             color = Image.new(img.mode, img.size, self.contour_color)

ValueError: operands could not be broadcast together with shapes (450,450,4) (450,450,3) ```

Versions

Linux-4.15.0-34-generic-x86_64-with-debian-buster-sid
Python 3.7.1 | packaged by conda-forge | (default, Nov 13 2018, 18:33:04)
[GCC 7.3.0]
NumPy 1.15.4
matplotlib 3.0.2
wordcoud 1.5.0.post10+gb79b3d6

@amueller amueller added the bug label Nov 6, 2019
@amueller
Copy link
Owner

amueller commented Nov 6, 2019

Thanks for reporting. Do you want to send a PR to fix it?

@amueller
Copy link
Owner

amueller commented Nov 6, 2019

this is fixed by #441 right?

@DannyBen
Copy link

DannyBen commented Sep 24, 2021

I hope this can still be fixed, as far as I can understand, it is still an issue.

Reproducible with:

$ wordcloud_cli \
  --text mytext.txt \
  --imagefile wordcloud.png \
  --contour_width 3 \
  --contour_color 'steelblue' \
  --mask alice.png \
  --mode RGBA

With --mode RGB it works.

I am not a python developer, so cannot help with the code I'm afraid.

@tirth78
Copy link

tirth78 commented Sep 9, 2022

I would like to work on this issue. I am a Masters student in BITS Pilani. It would be really helpful if I could get some kind of approval from the owner/author.

@caioems
Copy link

caioems commented Apr 5, 2024

This issue persists, has anyone found a workaround?

chaseleif added a commit to chaseleif/word_cloud that referenced this issue Jun 18, 2024
@chaseleif
Copy link

I was able to duplicate the issue using the command given by DannyBen and I submmitted a pull request with a solution.
@caioems, if you still need a workaround you can copy my changes and test it out.

@Mubashiropeyemi
Copy link

Mubashiropeyemi commented Jun 18, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants