word_cloud: Value Error: ImageColorGenerator is smaller than the canvas

Description

Hey I am definitely sure that my ImageColorGenerator is bigger than my canvas and I even tried to change the canvas width height to more and less but I am always getting this value error no matter what. Here is my code -

Example:

mask3 = np.array(Image.open(path.join(d, "test7.png")))
wc = WordCloud(background_color="white", max_words=1000, mask=mask3,stopwords=q,
               max_font_size=150, random_state=42)
wc.generate(str4)
# create coloring from image
image_colors = ImageColorGenerator(mask3)
print(image_colors.image.shape)
print(wc.width, wc.height)
plt.figure(figsize=[5,5])
plt.tight_layout(pad=0)
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.show()

Explanation

This Image test7 here is 800800 and I know that default canvas is 400200 I have another image which is 900*900 and that is working perfectly.

Can you explain me as to how this is working and what is happening:

output for test7 image ( the red thing with eyes)

(800, 800, 4) 400 200

Images

This is the image with which it is working perfectly test1 This is the image which is showing error test7 This is the image to which I actually want to do it ultimately this image is 500*306 test8

Please help

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 38 (21 by maintainers)

Most upvoted comments

Thanks for your reports but they would have been much easier to investigate if you’d have provided a minimum reproducing example, such as the 4 lines I gave above.

fyi: if you are stilling running into this error (“ValueError: ImageColorGenerator is smaller than the canvas”) when generating the word cloud from frequencies and using small font sizes, make sure there isn’t a blank string (‘’) in your word dictionary.

e.g., running this fixed things for me:

word_dict.pop('', None)
wc.generate_from_frequencies(word_dict)

in my case, the issue stemmed from a (0,0) box size associated with the blank string.

Data structure:

# Structure of word_dict
{
word1 : count,
word2 : count,
...
word_N : count
}

Full code

# IMPORT PACKAGES
import numpy as np
import random
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
from palettable.colorbrewer.qualitative import Dark2_8

# FONT, ICON, MASK
font = "a엄마의편지B"
font_path = "%s.ttf" % font

icon = "korea2"
icon_path = "%s.png" % icon

icon = Image.open(icon_path).convert("RGBA")
mask = Image.new("RGB", icon.size, (255,255,255))
mask.paste(icon,icon)
mask = np.array(mask)

# Word Cloud with colored mask
wc = WordCloud(font_path=font_path, background_color="white", max_words=20000, mask=mask,
               max_font_size=300, random_state=42)

coloring = np.array(Image.open(icon_path))
image_colors = ImageColorGenerator(coloring)
image_colors.default_color = [0.6,0.6,0.6] # Important!!! at 2018.07.07

# Generate word cloud
wc.generate_from_frequencies(word_dict)
wc.recolor(None,image_colors)
wc.to_file("output.png")

output

image