[IMAGE-SIG] Font types with PIL
Fri, 8 Aug 1997 14:42:22 -0400 (EDT)
>is there a way to write a character string onto an image and use
>different or personalized types of fonts?
>is there any limit for fonts?
There doesn't seem to be any support for fonts in 0.3a1,
though only Fredrik knows what he's implemented for future releases.
Assuming you have bitmaps for the characters, the paste() method of
Image objects could be used to add them to an image, but getting those
bitmaps is the tough part! You'd have to either convert font files to
a bunch of files that PIL can read, one file per character, or write
Python code to read the font files and generate bitmaps.
Actually, this would make a very interesting project for
someone looking to improve their Python/PIL skills, and would (mostly)
be feasible in pure Python. A possible design:
* An ImageWriter class analogous to the existing ImageDraw
class; ImageWriter requires the image to be written on, and an
instance of the Font class (see below). Various methods write one or
more characters to the image, updating the current position; there are
also utility methods to do things like fit a string of text into a
given box, move the current position, and so forth.
* Font objects encapsulate the information about a font, such
as the bitmap (and its size) for each character. Font objects could
also expose kerning information, which could be used by the
ImageWriter class in updating the current position after each
character. For example, if the previously written character was a
'T', the current character may be able to nestle underneath the
crossbar to some degree.
* Various functions which read font files and return a
corresponding Font object: ReadTTFont(), ReadAFMFont(), etc. This is
the only part which might have to be written in C, depending on the
complexity of the format and the computations required to render it.
Possible starting points:
* The FreeType project is a freeware library (under the LGPL)
to render TrueType fonts. A PIL interface to it would be quite useful.
* I have code to read TeX's gf-format files. I also once
wrote some code to read TrueType font files, but not to render them
(which is the complicated part!).
IMAGE-SIG - SIG on Image Processing with Python
send messages to: email@example.com
administrivia to: firstname.lastname@example.org