1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| print("正在准备中,请稍等...") import os import matplotlib.pyplot as plt from fontTools.ttLib import TTFont from matplotlib.font_manager import FontProperties import matplotlib.patheffects as path_effects
font_path = '1.ttf' output_dir = './output/' dpi = 60 hs = 92 hw = 4 hc = "black" ss = 100 sc = "black"
print("欢迎使用一个生成字体图片的小pyhton程序\n请提前在py文件里设置相应的参数\nmade by 天影大侠")
if not os.path.exists(output_dir): os.makedirs(output_dir) i = "欢迎给天影大侠点赞" while i == "欢迎给天影大侠点赞": input_text = input("请输入要生成图片的字符: ") print("正在生成中... 字符越多生成越久")
font = TTFont(font_path)
def render_character(char, is_hollow=False): valid_char = char.replace("?", "问号")
fig, ax = plt.subplots(figsize=(1.2, 2), dpi=dpi) ax.set_axis_off()
prop = FontProperties(fname=font_path)
if is_hollow: text = ax.text(0.5, 0.5, char, fontsize=hs, ha='center', va='center', fontproperties=prop, color='none', weight='bold') text.set_path_effects([ path_effects.withStroke(linewidth=hw, foreground=hc, capstyle='round', joinstyle='round') ]) else: ax.text(0.5, 0.5, char, fontsize=ss, ha='center', va='center', fontproperties=prop, color=sc, weight='bold')
plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
image_path = os.path.join(output_dir, f'{valid_char}_{"hollow" if is_hollow else "solid"}.png') plt.savefig(image_path, format='png', transparent=True) plt.close(fig)
for char in input_text: render_character(char, is_hollow=False) render_character(char, is_hollow=True)
print("所有字符图像已生成!")
|