将TrueType字体(TTF)中的字符(字形)转储到位图中 [英] Dump characters (glyphs) from TrueType font (TTF) into bitmaps
问题描述
我有一个自定义的TrueType字体(TTF),它由一堆图标组成,我想将其渲染为单独的位图(GIF,PNG等),以便在Web上使用.您会认为这是一个简单的任务,但是显然不是吗?这里有大量与TTF相关的软件:
I have a custom TrueType font (TTF) that consists of a bunch of icons, which I'd like to render as individual bitmaps (GIF, PNG, whatever) for use on the Web. You'd think this is a simple task, but apparently not? There is a huge slew of TTF-related software here:
http://cg.scs.carleton.ca/~luc/ttsoftware. html
但是,所有级别的不完全是我想要的",断开的链接和/或很难甚至不可能在现代Ubuntu机器上进行编译-例如. dumpglyphs (C ++)和
But it's all varying levels of "not quite what I want", broken links and/or hard to impossible to compile on a modern Ubuntu box -- eg. dumpglyphs (C++) and ttfgif (C) both fail to compile due to obscure missing dependencies. Any ideas?
推荐答案
import Image, ImageFont, ImageDraw
im = Image.new("RGB", (800, 600))
draw = ImageDraw.Draw(im)
# use a truetype font
font = ImageFont.truetype("path/to/font/Arial.ttf", 30)
draw.text((0, 0), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", font=font)
# remove unneccessory whitespaces if needed
im=im.crop(im.getbbox())
# write into file
im.save("img.png")
这篇关于将TrueType字体(TTF)中的字符(字形)转储到位图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!