Convert TrueType glyphs to PNG image?

Python3

Since nobody has really address the part that specifies for C++, Python, Ruby, or Perl, here is the Python3 way. 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}")

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&

probably partly duplicate of How can I convert TTF glyphs to .png files on the Mac for free?

imagemagick can fulfill this kind of requests, should works fine on Mac/Linux/Windows. :-)

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

if a batch convert is needed, maybe you can consider use a little ruby script called ttf2png .


The PIL provides an API for this, but it's easy to use. Once you've got the PIL image, you can export it.