import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
lines = ["Get me off", "your Fuck", "ing Mailing", "List"]
# get a nice heavy font
font = ImageFont.truetype("fonts/Lato-Black.ttf", size=200)
# calculate width
width = max(font.getbbox(line)[2] for line in lines)
# create BW image containing text
im = Image.new('1', (width, len(lines) * font.size))
draw = ImageDraw.Draw(im)
for i, line in enumerate(lines):
draw.text((0, i * font.size), line, font=font, fill=1)
# sample points
y, x = np.where(np.array(im) > 0)
ii = np.random.randint(len(x), size=sum(map(len, lines)) * 50)
x = x[ii] / im.width
y = y[ii] / im.height
# recreate figure
fig, ax = plt.subplots()
ax.semilogy()
ax.scatter(x, 10**(y*-5), 7**2, linewidths=0.5, marker='+', color='black')
ax.set_xlabel("Your Fucking Mailing List")
ax.set_ylabel("Get me off")
ax.set_title("Get me off Your Fucking Mailing List")
import matplotlib.pyplot as plt
from matplotlib.textpath import TextPath
from matplotlib.font_manager import FontProperties
from matplotlib.transforms import IdentityTransform
import numpy as np
ax = plt.subplot()
N = 7000
x = np.random.random(size=N)
y = np.random.random(size=N)
ax.scatter(x, y, marker='+', color='k', lw=0.5)
text = 'StackOverflow'
text = TextPath((60, 200), text,
prop=FontProperties(weight='bold', size=55),
)
ax.collections[0].set_clip_path(text, transform=IdentityTransform())
3条答案
按热度按时间gg58donl1#
既然网格包支持剪切路径,我们可以:
eqoofvh92#
如果你想真正生成文本中的随机点,你可以使用Python的Numpy和Pillow模块相对容易地完成。
这可能会产生如下结果:
由于没有屏蔽,因此很难看到字母,但考虑到您似乎想要用于聚类的点,这可能不是很重要。
u7up0aaq3#
使用matplotlib和裁剪,这并不容易处理多行(不幸的是):
输出: