Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pixel fonts in PIL

Tags:

python

imaging

I am creating images using PIL that contain numerous exactly placed text strings. My first attempt was to convert pixel fonts into the pil-compatible format as described here. For example, I download the Silkscreen font and convert it:

otf2bdf -p 8pt -o fonts/slkscr.bdf fonts/slkscr.ttf 
pilfont.py fonts/slkscr.bdf

I can then use the font in PIL like so:

import Image, ImageDraw, os, sys, ImageFont
im = Image.new("RGB", (40,10))
draw = ImageDraw.Draw(im)
fn = ImageFont.load('fonts/slkscr.pil')
draw.text((0,0), "Hello", font=fn)
del draw
# write to stdout
im.save(sys.stdout, "PNG")

However, the resulting image (alt text) does not reflect what the font should look like.

What procedure should I be using to convert and use pixel fonts so that they render as intended?

Thanks in advance.

like image 467
Vince Avatar asked Mar 06 '09 17:03

Vince


1 Answers

Eureka!

Just needed to specify a resolution of 72 dpi (default is 100) for otf2bdf:

otf2bdf -p 8 -r 72 -o fonts/slkscr.bdf fonts/slkscr.ttf

Now, alt text looks great!

like image 133
Vince Avatar answered Sep 29 '22 16:09

Vince