[Image-SIG] Embedding text in image files

Bernd Preusing b.preusing at web.de
Sun Apr 25 02:25:50 EDT 2004


On Sat, 24 Apr 2004 14:43:56 -0500, Kent Tenney <kent at springfed.com> wrote:

> Howdy,
>
> I'm using PIL 1.1.5a1 with Python 2.3.3 on Windows,
> and would like to put textual information in
> image files.
>
> What options are available for writing
> text to jpeg, gif and png ?

Hiho,

what do you mean by "options"?

IMHO the simplest way is something like this:

  from PIL import ImageDraw, ImageFont
# input: img, fontPath, text1
font1 = ImageFont.load(fontPath)
draw1 = ImageDraw.Draw(img)
tSizeX, tSizeY = draw1.textsize(text, font=font1)
# calculate x and y...
draw.text((x, y), text1, font=font1, fill='#000000')
... save img

I have used this with PIL 1.1.4 only.
To my experience text looks nicer on RGB images
(jpeg), so I convert palette images to RGB before
placing objects.

If you want more complex effects like shadow text
or transparent text, take a look at the filters page
and source code of PyAlbum http://pyalbum.sourceforge.net/Filters.html

Best regards
    Bernd



More information about the Image-SIG mailing list