12

是否有命令行工具可以将字形从 TTF 文件转换为 PNG(或其他一些位图图像格式)?

如果没有现成的命令行工具,您将如何从 C++、Perl、Python 或 Ruby 中的一种或在 Ubuntu 机器上很容易找到的东西中进行操作?

4

5 回答 5

13

可能部分重复如何在 Mac 上免费将 TTF 字形转换为 .png 文件?

imagemagick可以满足这种要求,应该可以在 Mac/Linux/Windows 上正常工作。:-)

convert -background none -fill black -font font.ttf -pointsize 300 label:"Z" z.png

如果需要批量转换,也许您可​​以考虑使用一个名为ttf2png的小红宝石脚本。

于 2014-07-15T09:25:24.320 回答
1

PIL 为此提供了一个API,但它很容易使用。获得 PIL 图像后,您可以将其导出。

于 2013-06-17T10:51:50.293 回答
1
wget http://sid.ethz.ch/debian/ttf2png/ttf2png-0.3.tar.gz
tar xvzf ttf2png-0.3.tar.gz
cd ttf2png-0.3 && make
./ttf2png ttf2png -l 11 -s 18 -e -o test.png /path/to/your/font.ttf
eog test.png&
于 2014-12-01T21:37:07.490 回答
1

正如@imcaspar 所建议的那样,但我需要它将 ttf 字体中的图标转换为具有特定大小的 png 以用于 ios 集成

convert -background none -font my-icons-font.ttf -fill black  -size "240x240"  label:"0" +repage -depth 8 icon0@1x.png

其中“0”是为我的任何图标映射的字符。额外的选项使我可以正确生成所有字符(图标),因为某些地方被常规命令裁剪(+repage -depth 完成了这项工作)

于 2016-06-20T15:59:58.093 回答
0

Python3

由于没有人真正解决为 C++、Python、Ruby 或 Perl 指定的部分,因此这里是 Python3 方式。我试图进行描述,但您可以简化以按照您的需要工作。

要求:PIL(枕头)

PILImageDrawImageFont模块

# pip install Pillow
from PIL import Image, ImageFont, ImageDraw

# use a truetype font (.ttf)
# font file from fonts.google.com (https://fonts.google.com/specimen/Courier+Prime?query=courier)
font_path = "fonts/Courier Prime/"
font_name = "CourierPrime-Regular.ttf"
out_path = font_path

font_size = 16 # px
font_color = "#000000" # HEX Black

# Create Font using PIL
font = ImageFont.truetype(font_path+font_name, font_size)

# Copy Desired Characters from Google Fonts Page and Paste into variable
desired_characters = "ABCČĆDĐEFGHIJKLMNOPQRSŠTUVWXYZŽabcčćdđefghijklmnopqrsštuvwxyzž1234567890‘?’“!”(%)[#]{@}/&\<-+÷×=>®©$€£¥¢:;,.*"

# Loop through the characters needed and save to desired location
for character in desired_characters:
    
    # Get text size of character
    width, height = font.getsize(character)
    
    # Create PNG Image with that size
    img = Image.new("RGBA", (width, height))
    draw = ImageDraw.Draw(img)
    
    # Draw the character
    draw.text((-2, 0), str(character), font=font, fill=font_color)
    
    # Save the character as png
    try:
        img.save(out_path + str(ord(character)) + ".png")
    except:

        print(f"[-] Couldn't Save:\t{character}")
于 2021-04-13T03:13:17.930 回答