[IMAGE-SIG] stamping strings

Fredrik Lundh fredrik.lundh@image.combitech.se
Thu, 14 Aug 1997 11:23:13 +0200

>Using PIL, is it possible to stamp a string into a given image? I'm
>experimenting with PSDraw and an not making any headway. Even if this
>strategy did work, it would be inferiour to just defining a string, a
>font, a position and and a method of stamping it...

Yes, it can be done, using the (undocumented) font support in
ImageDraw (see a recent post for an overview). Roughly, you'll
have to:

1) find a suitable font in the PILfont format (use "bdf2pil.py" to convert
BDF font files to this format, and the X utility "fstobdf" to extract suitable
fonts from your X server... if you use Windows, you can get BDF fonts
from the X11R6 distribution which are available in many places on the

(SMHI-specifikt: fast du kan fvrstes titta i den senaste itip-distributionen
pe earth, jag tror det ska finnas ett gdng konverterade fonter i ett under-
bibliotek som heter BDF)

2) load the font using the stuff in the ImageFont module

3) create an ImageDraw object, and use the setfont method to
set the font

4) use the text method to draw the actual text into the image

Caveat: as of 0.3a1, the ImageDraw package still doesn't work on
RGB/CMYK images. In this case, you can draw into a mode 1 image,
create a blank image (initialized to the appropriate colour) with the
same size as the drawing, and use paste to paste the colour onto
the original image, using the drawing as a bitmap.

I'm afraid you have to use the source for more details; when I get
the time, I'll post something on various ways to combine draw and
paste to do overlays, watermarks, etc.

Cheers /F

IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org