赞
踩
要做特定mask形状的古诗词的词云,需要竖排。但wordcloud的竖排文字都是左转90度躺平的。
搜了csdn博客和c一下,没有找到可用的代码。只有一个博客说可以给每个字添加'\n',看原理和效果图应该是失败了。
a. mask图片可以预先左转90度,等词云图片生成后,再右转90度恢复。这个可以在使用wordcloud的前后实施。
b. 实际需求转变为生成全部为正常的水平方向的词云,但每个字左转90度。
c. 解读wordcloud.py后,采用给WordCloud类增加一个参数vert,并修改init()、generate_from_frequencies()、to_image()这三个成员函数。
win10+python3.9+wordcloud1.8.1
预先下载一个中文TrueType字体库,并在wordcloud.py里设置FONT_PATH
FONT_PATH = os.environ.get('FONT_PATH', os.path.join(FILE, 'fangsong_GB2312.ttf'))
注意:PIL中 Transpose.ROTATE_90 已代替 ROTATE_90
- from PIL import Image
- from wordcloud import WordCloud
- img=Image.open(os.path.join(path,'shape_heart.png'))
- if vert :
- img=img.transpose(Image.Transpose.ROTATE_90)
- shape_mask = np.array(img)
-
- '''
- # get wordcloud (透明背景,粉色)
- wc = WordCloud(background_color=None, mode='RGBA', scale=1.0, vert=vert,
- color_func=lambda *args, **kwargs: "pink",max_font_size=22,min_font_size=14,
- prefer_horizontal=1, contour_color=None, mask=shape_mask)
- wc.generate(text)
- img=wc.to_image()
- '''
- if vert:
- img=img.transpose(Image.Transpose.ROTATE_270)
- # 保存词云图
- img.save(fOut)
增加class 参数 vert
- def __init__(self, font_path=None, width=400, height=200, margin=2,
- ranks_only=None, prefer_horizontal=.9, mask=None, scale=1,
- color_func=None, max_words=200, min_font_size=4,
- stopwords=None, random_state=None, background_color='black',
- max_font_size=None, font_step=1, mode="RGB",
- relative_scaling='auto', regexp=None, collocations=True,
- colormap=None, normalize_plurals=True, contour_width=0,
- contour_color='black', repeat=False,
- include_numbers=False, vert=False,min_word_length=0, collocation_threshold=30):
赋值
self.vert = vert
这是generate系列函数的最底层,
包括安排布局,把获得的x,y=np.array(result),存于layout的position,
修改它的目的在于适应字体转向后的高宽差异。
- # get size of resulting text
- box_size = draw.textsize(word, font=transposed_font)
- # find possible places using integral image:
- result = occupancy.sample_position(box_size[1] + self.margin,
- box_size[0] + self.margin,
- random_state)
-
修改为
- # get size of resulting text
- box_w,box_h = draw.textsize(word, font=transposed_font)
- if self.vert and orientation==None:
- font_w,font_h = font.getsize("国")
- box_w = box_w *font_h//font_w
- box_h = box_h *font_w//font_h
- # find possible places using integral image:
- result = occupancy.sample_position(box_h + self.margin,
- box_w + self.margin,
- random_state)
其中获取字体高宽信息使用了font.getsize("国"),font为
- from PIL import ImageFont
- font = ImageFont.truetype(self.font_path, font_size)
这个是实际输出的函数
- for (word, count), font_size, position, orientation, color in self.layout_:
- font = ImageFont.truetype(self.font_path,
- int(font_size * self.scale))
- transposed_font = ImageFont.TransposedFont(
- font, orientation=orientation)
- pos = (int(position[1] * self.scale),
- int(position[0] * self.scale))
- draw.text(pos, word, fill=color, font=transposed_font)
修改为
- for (word, count), font_size, position, orientation, color in self.layout_:
- font = ImageFont.truetype(self.font_path,
- int(font_size * self.scale))
- font_w,font_h = font.getsize("国")
- transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
- vert90_font = ImageFont.TransposedFont(font, orientation=Image.ROTATE_90)
-
- x = int(position[0]* self.scale)
- y = int(position[1]* self.scale)
- if not self.vert or orientation!=None:
- draw.text((y,x), word, fill=color, font=transposed_font)
- else :
- for i in range(len(word)):
- draw.text((y+font_h*i, x), word[i], fill=color, font=vert90_font)
vert=False
vert=True
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。