我无法让自动跟踪工作,所以我编写了一个 python 脚本来将黑白图像跟踪到矢量多边形中,每个像素一个。
因此,使用字形图像文件夹,您可以将其转换为文件svg
文件夹。
然后在 Inkscape union + 简化路径以清理每个字形。(可能在 Inkscape 命令行上可行)
svg
最后为每个字形导入相应的FontForge。
-> 托管在gist.gisthub.com上的脚本
from PIL import Image
import svgwrite
import os
import argparse
def bmp_to_svg(filename, multiply =1):
img = Image.open(filename)
w, h = img.size
pixel_acc = img.load()
generated_paths = paths_gen(pixel_acc, w,h, multiply=multiply)
dwg = svgwrite.Drawing(profile='tiny')
for path in generated_paths:
options = path
kwoptions = {} #{"fill":"black"}
dwg.add(dwg.polygon(options, **kwoptions))
#clip_path = dwg.defs.add(dwg.clipPath())
#clip_path.add(dwg.Rect(insert=(0,0), size=((w*multiply)+1, (h*multiply)+1)))
return dwg
def paths_gen(pixel_acc, w, h, multiply = 1):
m = multiply
paths = []
for y in range(0,h):
for x in range(0,w):
pix = pixel_acc[x,y]
brightval = sum(pix) /3
if brightval < 128:
paths.append([
##top left
(x * m, y * m),
##top right
((x+1) * m, y * m),
##bottom right
((x+1) * m, (y+1) * m),
##bottom list
((x) * m, (y+1) * m),
##close back to top left
#(x, y),
])
return paths
def main():
parser = argparse.ArgumentParser(
description='Script to convert black and white images into vector art')
parser.add_argument("input_dir")
parser.add_argument("ouput_dir")
parser.add_argument("in_ext", default="bmp", help="file extension of images in input folder")
parser.add_argument("-m", "--multiply", default=1, type=int)
## TODO
#parser.add_argument("-u", "--union", store=True, help="unifies adjecent polgons")
#parser.add_argument("-s", "--simplify", store=True, help="removes points that are part of a straight line")
#parser.add_argument("-c", "--compress", store=True, help="same as '--union --simplify'")
args = parser.parse_args()
cdir = os.path.abspath(args.input_dir)
fn_gen = ( x for x in os.listdir(cdir)
if x.endswith('.'+args.in_ext)
)
for count, filename in enumerate(fn_gen):
full_filename = os.path.join(cdir, filename)
new_fn = "{}_.svg".format(filename)
new_fullfn = os.path.join(args.ouput_dir, new_fn)
svg = bmp_to_svg(full_filename, multiply=args.multiply)
print "Converted file: '{}'".format(full_filename)
svg.filename = new_fullfn
svg.save()
if __name__ == '__main__':
main()