当前位置: 代码网 > it编程>前端脚本>Python > Python实现生成书法字体的示例代码

Python实现生成书法字体的示例代码

2025年11月28日 Python 我要评论
本文介绍了一个使用matplotlib生成书法文字图片的python方法。通过导入matplotlib库,定义create_calligraphy_with_matplotlib函数,可以指定文本内容

本文介绍了一个使用matplotlib生成书法文字图片的python方法。通过导入matplotlib库,定义create_calligraphy_with_matplotlib函数,可以指定文本内容、字体路径、输出路径等参数,设置字体属性并绘制书法文字。函数会隐藏坐标轴,保存为透明背景的图片。示例代码演示了如何使用该函数生成"水墨丹青"的书法作品,并可自定义字号和颜色。该方案适合需要将书法文字可视化的应用场景。

效果如下:

完整代码: 

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

def create_calligraphy_with_matplotlib(text, font_path, output_path, size=50, color='black'):
    """
    使用matplotlib生成书法文字图片
    """
    # 设置字体
    prop = fm.fontproperties(fname=font_path, size=size)
    
    # 创建图形
    fig, ax = plt.subplots(figsize=(10, 3))
    ax.text(0.5, 0.5, text, fontproperties=prop, 
            ha='center', va='center', color=color, size=size)
    
    # 隐藏坐标轴
    ax.axis('off')
    
    # 保存图片
    plt.savefig(output_path, bbox_inches='tight', pad_inches=0.1, transparent=true)
    plt.close()
    print(f"书法文字图片已保存至: {output_path}")

# 使用示例
if __name__ == "__main__":
    font_path = "droidsansfallback.ttf"  # 替换为你的书法字体路径
    text = "水墨丹青"
    output_path = "calligraphy_matplotlib.png"
    
    create_calligraphy_with_matplotlib(text, font_path, output_path, size=80, color='#8b4513')

知识扩展:

下面我们来看看如何使用python 模拟自己的手写字体吧

方式一

1.首先去网站生成自己的字体文件(ttf格式的文件)系统自带的字体文件在:c:\windows\fonts

2.通过代码实现手写字体:

# coding: utf-8
from pil import image, imagefont

from handright import template, handwrite

text = "你好,世界"

template = template(
    background=image.new(mode="1", size=(900, 1000), color=1),
    font=imagefont.truetype("c:/windows/fonts/stliti.ttf", size=100),
    # line_spacing=150,
    # fill=0,  # 字体“颜色”
    # left_margin=100,
    # top_margin=100,
    # right_margin=100,
    # bottom_margin=100,
    # word_spacing=15,
    # line_spacing_sigma=6,  # 行间距随机扰动
    # font_size_sigma=20,  # 字体大小随机扰动
    # word_spacing_sigma=3,  # 字间距随机扰动
    # end_chars=",。",  # 防止特定字符因排版算法的自动换行而出现在行首
    # perturb_x_sigma=4,  # 笔画横向偏移随机扰动
    # perturb_y_sigma=4,  # 笔画纵向偏移随机扰动
    # perturb_theta_sigma=0.05,  # 笔画旋转偏移随机扰动
)
images = handwrite(text, template)
for i, im in enumerate(images):
    assert isinstance(im, image.image)
    im.show()
    # im.save(r"c:\users\zhichao\{}.webp".format(i))

 效果:

感觉不好看,换了几个字体了也一样。

方式二

from requests import get


text = "hello,world"
params = {
    'text': text,
}

if len(text) > 1035:
    print('the content you entered is too long.')
else:
    try:
        res = get('https://pywhatkit.herokuapp.com/handwriting', params=params)
        with open('text.png', 'wb') as f:
            f.write(res.content)
        print('successful production.')
    except exception as e:
        print('error :', e)

效果:

该方式貌似只支持英文。。。

到此这篇关于python实现生成书法字体的示例代码的文章就介绍到这了,更多相关python生成书法字体内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com