如何将单个BMP像素字符转换为TTF/OTF字体文件?
将单个字符SVG合并为TTF/OTF字体文件的方法
我正在使用Pillow制作像素字体,该工具可导出*.bmp文件。我希望将单个字符的*.bmp转换为*.ttf或*.otf格式。目前示例包含字符a(Unicode 0061)和p(Unicode 0070),后续会扩展至完整字母表。我已将BMP转为SVG,了解到Glyphs Mini 2、Font Forge等字体工具可导入SVG,但不知如何将单个SVG合并为有效的TTF/OTF字体文件。
以下是我将数组转为BMP再转SVG的Python代码示例:
from PIL import Image, ImageDraw from potrace import Bitmap import numpy as np import os # p p = np.array( [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 1, 0], [1, 0, 0, 1, 0], [1, 1, 1, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ] ) # a a = np.array( [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 1, 0], [1, 0, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ] ) dir_bmp = "bmp/" dir_svg = "svg/" def array_to_drawing(letter_name: str, array: np.array): """Converts a numpy array to a drawing. Args: letter_name (str): The name of the letter to save the image as. array (np.array): The 2D numpy array to convert. """ # Create an image with a white background width = 320 height = 1024 img = Image.new("1", (width, height), color="white") # Create a draw object to draw on the image draw = ImageDraw.Draw(img) # Draw the letter which the 2d numpy array represents for row in range(array.shape[0]): for col in range(array.shape[1]): if array[row, col] == 1: x0 = col * 64 y0 = row * 64 draw.rectangle([x0, y0, x0 + 64, y0 - 64], fill="black") img.save(dir_bmp + letter_name + ".bmp") print( "potrace " + dir_bmp + letter_name + ".bmp --svg -o " + dir_svg + letter_name + ".svg" ) os.system( "potrace " + dir_bmp + letter_name + ".bmp --svg -o " + dir_svg + letter_name + ".svg" ) array_to_drawing("p", p) array_to_drawing("a", a)
字符预览
- 字符p:

- 字符a:

内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

