Linux 是否将TrueType图示符转换为PNG图像?

Linux 是否将TrueType图示符转换为PNG图像?,linux,bitmap,rendering,command-line-interface,truetype,Linux,Bitmap,Rendering,Command Line Interface,Truetype,是否有命令行工具将TTF文件中的字形转换为PNG(或其他位图图像格式) 如果没有准备好的命令行工具,你会如何从C++、Perl、Python或露比中的某个地方或在Ubuntu框中找到的东西?< p>,pIL提供了一个,但是它很容易使用。获得PIL图像后,可以将其导出 可能部分重复了 能够满足此类请求,在Mac/Linux/Windows上应该可以正常工作。:-) convert-background none-fill black-font-font.ttf-pointsize 300标签:“Z

是否有命令行工具将TTF文件中的字形转换为PNG(或其他位图图像格式)


如果没有准备好的命令行工具,你会如何从C++、Perl、Python或露比中的某个地方或在Ubuntu框中找到的东西?

< p>,pIL提供了一个,但是它很容易使用。获得PIL图像后,可以将其导出

可能部分重复了

能够满足此类请求,在Mac/Linux/Windows上应该可以正常工作。:-)

convert-background none-fill black-font-font.ttf-pointsize 300标签:“Z”Z.png

如果需要批量转换,也许可以考虑使用一个小的Ruby脚本,如@ I/P>< P>,如@ IMACASPAR所建议的,但是我需要它将TTF字体中的图标转换成具有特定尺寸的PNG,用于iOS集成< /P>

convert -background none -font my-icons-font.ttf -fill black  -size "240x240"  label:"0" +repage -depth 8 icon0@1x.png
其中“0”是为我的任何图标映射的字符。额外的选项允许我将所有字符(图标)正确生成为使用常规命令裁剪的部分(+repage-depth完成了工作)

Python3

因为没有人真正地解决了C++、Python、Ruby或Perl指定的部分,这里是Python 3方式。我试着描述一下,但你可以简化工作,按照你的需要

要求:PIL(枕头) 's和模块

#pip安装枕
从PIL导入图像、ImageFont、ImageDraw
#使用truetype字体(.ttf)
#来自fonts.google.com的字体文件(https://fonts.google.com/specimen/Courier+Prime(查询=快递)
font\u path=“font/Courier Prime/”
font\u name=“CourierPrime Regular.ttf”
输出路径=字体路径
font_size=16#px
font#u color=“#000000”#十六进制黑色
#使用PIL创建字体
font=ImageFont.truetype(字体路径+字体名称、字体大小)
#从Google字体页面复制所需字符并粘贴到变量中
所需字符=“ABCĆDĐEFGHIJKLMNOPQRSŠTUVWXYZŽABCčDđEFGHIJKLMNOPQRSšTUVWXYZž1234567890'?”!“()[ţ]{TUVWXYZ
#循环浏览所需字符并保存到所需位置
对于所需字符中的字符:
#获取字符的文本大小
宽度,高度=font.getsize(字符)
#创建具有该大小的PNG图像
img=Image.new(“RGBA”(宽度、高度))
draw=ImageDraw.draw(img)
#画人物
draw.text(-2,0),str(字符),font=font,fill=font\u颜色)
#将角色另存为png
尝试:
img.save(out_path+str(ord(character))+“.png”)
除:
打印(f“[-]无法保存:\t{character}”)

这其中可能有一个答案:很好的答案,但所有OSX都是特定的,甚至是ruby。谢谢你@可复制的
convert -background none -font my-icons-font.ttf -fill black  -size "240x240"  label:"0" +repage -depth 8 icon0@1x.png
# 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}")