7

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.

Jochem Schulenklopper
  • 6,452
  • 4
  • 44
  • 62
Vince
  • 3,325
  • 2
  • 23
  • 41
  • Edit: I couldnt get it to work, and after a few hours I figured out that the pilfont.py utility actually creates 2 files, a .pil and a .pdm (the bdf is only a n intermediary). You need both of these files not only the .pil -.- – Tom Nov 16 '14 at 01:23
  • For future reference, the popular [bitfontmaker](https://www.pentacom.jp/pentacom/bitfontmaker2/) ttf can be put through this process. otf2bdf can be found [here](http://sofia.nmsu.edu/~mleisher/Software/otf2bdf/) (builds just fine _with_ patch as described on website). You will have to fidget with -p but don't need to specify resolution. – Schemetrical May 30 '22 at 05:29

1 Answers1

4

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!

Spooky
  • 2,966
  • 8
  • 27
  • 41
Vince
  • 3,325
  • 2
  • 23
  • 41
  • Do you know why that is? It doesn't seem logical that fewer dots per inch would give you a better resolution/rendering. – tgray Mar 06 '09 at 20:22
  • It's just a question of what size in points the font arbitrarily decides to match to a pixel. Silkscreen chose 72dpi as it is the old-school Mac OS resolution. – bobince Mar 07 '09 at 00:50
  • How can I see that Silkscreen uses 72dpi? Is that visible anywhere in the file? – Konstantin Schubert Feb 12 '21 at 19:52
  • Do not recall, as this was quite a while ago! Knowing my past (and current!) self, I suspect I tried various dpi until I found one that worked! – Vince Feb 12 '21 at 20:20