In [1]:
# modules for generating the word cloud
from os import path, getcwd
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
In [2]:
import os
def get_data_from_files(path):
    directory = os.listdir(path)
    results = []
    for file in directory:
        f=open(path+file)
        results.append(f.read())
        f.close()
    return results

filtered_texts = get_data_from_files('../NEG_JK/')
In [11]:
d = getcwd()
## join all documents in corpus
text = " ".join(filtered_texts)
## image from PublicDomainPictures.net
## http://www.publicdomainpictures.net/view-image.php?image=232185&picture=family-gathering
mask = np.array(Image.open(path.join(d, "family-gathering.png")))
# mask = np.array(Image.open(path.join(d, "questionmark.png")))
wc = WordCloud(background_color="white", max_words=1000, mask=mask,
               max_font_size=90, random_state=42)
wc.generate(text)
# create coloring from image
image_colors = ImageColorGenerator(mask)
plt.figure(figsize=[7,7])
plt.imshow(text.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
_=plt.show()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-2f2a71359233> in <module>
     12 image_colors = ImageColorGenerator(mask)
     13 plt.figure(figsize=[7,7])
---> 14 plt.imshow(text.recolor(color_func=image_colors), interpolation="bilinear")
     15 plt.axis("off")
     16 _=plt.show()

AttributeError: 'str' object has no attribute 'recolor'
<Figure size 504x504 with 0 Axes>
In [16]:
from PIL import Image, ImageDraw, ImageFont
 
mask = np.array(Image.open(path.join(d, "family-gathering.png")))
# img = Image.new('RGB', (100, 30), color = (73, 109, 137))
# img = Image.new(mask, (100,30) color=(73,109,137))
 
fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
d = ImageDraw.Draw(mask)
d.text((10,10), "Hello world", font=fnt, fill=(255, 255, 0))
 
img.save('pil_text_font.png')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.7/site-packages/PIL/ImageDraw.py in Draw(im, mode)
    465     try:
--> 466         return im.getdraw(mode)
    467     except AttributeError:

AttributeError: 'numpy.ndarray' object has no attribute 'getdraw'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-3a0daa683956> in <module>
      6 
      7 fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
----> 8 d = ImageDraw.Draw(mask)
      9 d.text((10,10), "Hello world", font=fnt, fill=(255, 255, 0))
     10 

/usr/local/lib/python3.7/site-packages/PIL/ImageDraw.py in Draw(im, mode)
    466         return im.getdraw(mode)
    467     except AttributeError:
--> 468         return ImageDraw(im, mode)
    469 
    470 

/usr/local/lib/python3.7/site-packages/PIL/ImageDraw.py in __init__(self, im, mode)
     58            defaults to the mode of the image.
     59         """
---> 60         im.load()
     61         if im.readonly:
     62             im._copy()  # make it writeable

AttributeError: 'numpy.ndarray' object has no attribute 'load'
In [6]:
img
Out[6]:
In [18]:
from PIL import Image, ImageDraw
mask = np.array(Image.open("family-gathering.png"))
d = ImageDraw.Draw(mask)
d.text((10,10), "Let's see if this works. I like to eat apples", fill=(255,255,0))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.7/site-packages/PIL/ImageDraw.py in Draw(im, mode)
    465     try:
--> 466         return im.getdraw(mode)
    467     except AttributeError:

AttributeError: 'numpy.ndarray' object has no attribute 'getdraw'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-18-eb7f918f8b19> in <module>
      1 from PIL import Image, ImageDraw
      2 mask = np.array(Image.open("family-gathering.png"))
----> 3 d = ImageDraw.Draw(mask)
      4 d.text((10,10), "Let's see if this works. I like to eat apples", fill=(255,255,0))

/usr/local/lib/python3.7/site-packages/PIL/ImageDraw.py in Draw(im, mode)
    466         return im.getdraw(mode)
    467     except AttributeError:
--> 468         return ImageDraw(im, mode)
    469 
    470 

/usr/local/lib/python3.7/site-packages/PIL/ImageDraw.py in __init__(self, im, mode)
     58            defaults to the mode of the image.
     59         """
---> 60         im.load()
     61         if im.readonly:
     62             im._copy()  # make it writeable

AttributeError: 'numpy.ndarray' object has no attribute 'load'
In [ ]: