TrueType (TTF) dump characters (glyphs) to bitmaps

I have my own TrueType (TTF) font, which consists of many icons that I would like to display as separate bitmap images (GIF, PNG, whatever) for use on the Internet. You think this is a simple task, but apparently not? Here is a huge amount of TTF related software:

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

But all the different levels of "not quite what I want", broken links and / or hard to access to compile on the modern Ubuntu field - for example. dumpglyphs (C ++) and ttfgif (C) both fail to compile due to obscure missing dependencies. Any ideas?

+3
source share
4 answers

Try PIL ImageDraw and ImageFont module

The code will 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")
+5
source

Here's a working implementation of S. Mark's answer, which unloads the characters "a" into "z" in black at the correct PNG size:

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")
+2
source

( ):

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

, PIL ImagingCore, font.getmask().

+2

, Gimp, , . , , .

-1

Source: https://habr.com/ru/post/1741873/


All Articles