amueller / word_cloud

A little word cloud generator in Python
https://amueller.github.io/word_cloud
MIT License
10.08k stars 2.31k forks source link

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

Open rdelassus opened 4 years ago

rdelassus commented 4 years ago

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 commented 4 years ago

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

amueller commented 4 years ago

this is fixed by #441 right?

DannyBen commented 2 years ago

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 commented 2 years ago

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 commented 5 months ago

This issue persists, has anyone found a workaround?

chaseleif commented 2 months ago

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 commented 2 months ago

good day all I will need assistance with coding for weather prediction(heatwave) What is the data that may be needed? Which code system is the best?

On Tue, Jun 18, 2024 at 8:04 AM Chase Phelps @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/amueller/word_cloud/issues/501#issuecomment-2175312272, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANJNMOAUT3FWMSHPIKIZCSTZH7LZLAVCNFSM4JEC2YVKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMJXGUZTCMRSG4ZA . You are receiving this because you are subscribed to this thread.Message ID: @.***>