将 TrueType 字体 (TTF) 中的字符(字形)转储到位图中

发布于 2024-08-29 03:55:02 字数 505 浏览 7 评论 0原文

我有一个自定义 TrueType 字体 (TTF),它由一堆图标组成,我想将其渲染为单独的位图(GIF、PNG 等)以在 Web 上使用。您可能认为这是一个简单的任务,但显然不是?这里有大量与 TTF 相关的软件:

http://cg.scs。 carleton.ca/~luc/ttsoftware.html

但这都是不同程度的“不完全是我想要的”,损坏的链接和/或很难甚至不可能在现代 Ubuntu 机器上编译 - 例如。 dumpglyphs (C++) 和 ttfgif (C) 由于模糊的缺少依赖项而无法编译。有什么想法吗?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扭转时空 2024-09-05 03:55:02

尝试 PILImageDrawImageFont 模块

代码是这样的

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")

Try PIL's ImageDraw and ImageFont module

Code would be something like this

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")
小兔几 2024-09-05 03:55:02

其他答案的更简洁、更可靠的版本(为我剪掉了一些字形的一部分):

import string

from PIL import Image, ImageFont


point_size = 16
font = ImageFont.truetype("font.ttf", point_size)

for char in string.lowercase:
    im = Image.Image()._new(font.getmask(char))
    im.save(char + ".bmp")

我很想知道是否有更好的方法从 ImagingCore 对象构建 PIL 图像 font .getmask() 返回。

A more concise, and more reliable version of the other answers (which cut off parts of some glyphs for me):

import string

from PIL import Image, ImageFont


point_size = 16
font = ImageFont.truetype("font.ttf", point_size)

for char in string.lowercase:
    im = Image.Image()._new(font.getmask(char))
    im.save(char + ".bmp")

I’d be interested to know whether there’s a better way to construct a PIL Image from the ImagingCore object that font.getmask() returns.

她如夕阳 2024-09-05 03:55:02

这是 S.Mark 答案的一个有效实现,它将黑色的字符“a”到“z”转储到正确大小的 PNG 中:

import Image, ImageFont, ImageDraw

# use a truetype font
font = ImageFont.truetype("font.ttf", 16)
im = Image.new("RGBA", (16, 16))
draw = ImageDraw.Draw(im)

for code in range(ord('a'), ord('z') + 1):
  w, h = draw.textsize(chr(code), font=font)
  im = Image.new("RGBA", (w, h))
  draw = ImageDraw.Draw(im)
  draw.text((-2, 0), chr(code), font=font, fill="#000000")
  im.save(chr(code) + ".png")

Here's a working implementation of S.Mark's answer that dumps out chars 'a' to 'z' in black into correctly-sized PNGs:

import Image, ImageFont, ImageDraw

# use a truetype font
font = ImageFont.truetype("font.ttf", 16)
im = Image.new("RGBA", (16, 16))
draw = ImageDraw.Draw(im)

for code in range(ord('a'), ord('z') + 1):
  w, h = draw.textsize(chr(code), font=font)
  im = Image.new("RGBA", (w, h))
  draw = ImageDraw.Draw(im)
  draw.text((-2, 0), chr(code), font=font, fill="#000000")
  im.save(chr(code) + ".png")
故事↓在人 2024-09-05 03:55:02

Python3

工作实现了 S.Mark 的上述答案,但在字体文件和字体文件中添加了更多注释、变量和示例。人物。
我试图进行描述,但您可以根据需要进行简化。

要求:PIL(Pillow)

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}")

Python3

Working implementation of S.Mark's answer of the above, but added some more comments, variables, and examples to font files & characters.
I've tried to be descriptive, but you can simplify to work how you need it to.

Requirements: PIL (Pillow)

PIL's ImageDraw and ImageFont module

# 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}")
公布 2024-09-05 03:55:02

使用一些成像软件(例如 Gimp)显示您感兴趣的所有字符,然后将每个字符保存到文件中。不快也不高效,但你知道你会得到什么。

Use some imaging software like the Gimp to display all the characters you're interested in, then save each one to a file. Not fast or efficient, but you know what you'll be getting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文