Generating images with text in them

P

phil hunt

I am trying to generate some images (gifs or pngs) with text in
them. I can use the Python Imaging Library, but it only has access
to the default, rather crappy, font.

Ideally I'd like to use one of the nicer fonts that come with my X
Windows installation. Using Tkinter I can draw these fonts on the
screen; is there any way to get these fonts into a bitmapped image?
For example, can I draw some text on a canvas and then "grab" that
canvas as a bitmap into PIL, and then save it as a file?

Alternately, is there a good source of PIL font files (.pil files)
somewhere?

If the writers of the Python Imaging Library are reading this, may I
suggest that they add more fonts to it. Yes, that would increase
the size, but these days disk space is cheap and programmer time
expensive.
 
T

Terry Hancock

I am trying to generate some images (gifs or pngs) with text in
them. I can use the Python Imaging Library, but it only has access
to the default, rather crappy, font.

On the fly, or just during development?

In any case, you should be aware of the Skencil vector graphic
program which is written in Python (with some C extensions),
and which is also, of course, a python vector-graphics library.

Unfortunately, getting it to work in a server environment might
not be too pretty (requires GTK, etc, even if you don't actually
use it). I tried to make a stripped down version that didn't
require the desktop stuff, but it hasn't worked out so well yet.
Alternately, is there a good source of PIL font files (.pil files)
somewhere?

I believe there is a utility for converting other types of fonts, you
might have to go through a couple of different conversions from
the font files you have.
If the writers of the Python Imaging Library are reading this, may I
suggest that they add more fonts to it. Yes, that would increase
the size, but these days disk space is cheap and programmer time
expensive.

While bitmap font files are not copyrightable, there are license issues
with most of the "nicer" fonts you are probably talking about. That
complicates bundling them with the software. The PIL site does
actually have some additional fonts for download, though, IIRC.
 
D

Daren Russell

phil said:
I am trying to generate some images (gifs or pngs) with text in
them. I can use the Python Imaging Library, but it only has access
to the default, rather crappy, font.

Ideally I'd like to use one of the nicer fonts that come with my X
Windows installation. Using Tkinter I can draw these fonts on the
screen; is there any way to get these fonts into a bitmapped image?
For example, can I draw some text on a canvas and then "grab" that
canvas as a bitmap into PIL, and then save it as a file?

Alternately, is there a good source of PIL font files (.pil files)
somewhere?

If the writers of the Python Imaging Library are reading this, may I
suggest that they add more fonts to it. Yes, that would increase
the size, but these days disk space is cheap and programmer time
expensive.

I've just been playing around with this. You can use truetype fonts with:

font = ImageFont.truetype("/path/to/font.ttf", 12)

from version 1.1.4

http://www.pythonware.com/library/pil/handbook/imagefont.htm for more
details

HTH
Daren
 
P

phil hunt

On the fly, or just during development?

Just during development.
In any case, you should be aware of the Skencil vector graphic
program which is written in Python (with some C extensions),
and which is also, of course, a python vector-graphics library.

I may look that up, if I don't get joy with PIL or Tkinter.
Unfortunately, getting it to work in a server environment might
not be too pretty (requires GTK, etc, even if you don't actually
use it). I tried to make a stripped down version that didn't
require the desktop stuff, but it hasn't worked out so well yet.

I'm runnnig a desktop environment. For example Tkinter works fine.
I believe there is a utility for converting other types of fonts, you
might have to go through a couple of different conversions from
the font files you have.

I'm aware there's a utility, and I wish to avoid the hassle of
"go[ing] through a couple of different conversions from the font
files you have"
While bitmap font files are not copyrightable, there are license issues
with most of the "nicer" fonts you are probably talking about.

Oh? I can understand them being copyrighted; but if they are not
copywritable, what licnese issues are there? In any case, there
presumably are not license issues with the fonts that come with a
standard GNU/Linux distribution such as SuSE 9.1, which is what I am
using.
That
complicates bundling them with the software. The PIL site does
actually have some additional fonts for download, though, IIRC.

Do yuo have a URL?
 
M

Mike Meyer

Oh? I can understand them being copyrighted; but if they are not
copywritable, what licnese issues are there? In any case, there
presumably are not license issues with the fonts that come with a
standard GNU/Linux distribution such as SuSE 9.1, which is what I am
using.

I haven't looked in a while; the situation may have changed.

The font files per se are copyrightable, and you may have licensing
issues if you want to give those away. The *output* from the font
files - the bitmaps that Terry mentions - are not copyrightable. You
are free to do whatever you want with the images you get from them,
including scan them all in and create a bitmapped font file, or scan
them in in multipe sizes and cons up a competing font.

<mike
 
T

Terry Hancock

I've just been playing around with this. You can use truetype fonts with:

font = ImageFont.truetype("/path/to/font.ttf", 12)

from version 1.1.4

http://www.pythonware.com/library/pil/handbook/imagefont.htm for more

Wow, I didn't know about that new feature. I gotta use that. ;-)
I'd say that if you can do that, it's probably not worth messing with
the PIL fonts. There are lots of suitable free TrueType fonts available
on the web (many of which are also free-licensed, IIRC).
 
T

Terry Hancock

Just during development.

There are several free vector graphics programs. It would be fairly
trivial to hack a Skencil file to change text in it. Could do the
same with an SVG file from Inkscape. This would be a way to
generate your graphics.

I wrote a build utility for image resources based on Skencil which
creates images as part of your build process. See:
http://buildimage.narya.net

That might be useful to you.
I'm runnnig a desktop environment. For example Tkinter works fine.

Then Skencil is no problem if you want to use that.
Oh? I can understand them being copyrighted; but if they are not
copywritable, what licnese issues are there? In any case, there
presumably are not license issues with the fonts that come with a
standard GNU/Linux distribution such as SuSE 9.1, which is what I am
using.

*Bitmap* fonts are not copyrightable.* TrueType and Type 1 fonts are
outline fonts and are copyrightable just like programs (indeed they
technically are a kind of limited-domain program due to the coding of
"hints").

I see that the new ability to use True Type fonts solved your problem,
so I'll just leave it at that. Glad you found a solution.

Cheers,
Terry


*In the US anyway -- they are regarded as too simple. This might not
be true in other countries, it's clearly a judgement call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top