fonts in PIL ?

  • Thread starter Stéphane Ninin
  • Start date
S

Stéphane Ninin

Hello all,

I am writing (on Linux Redhat 9)
some script to make graphs using PIL, from a set of points.

All is ok, except that there seems to be no font coming with PIL.
Where could I fond some ?
Also, are there some real examples of use of fonts in PIL ?

Documentation says something like:
load
ImageFont.load(file) => Font instance

Loads a font from the given file, and returns the corresponding font
object. If this function fails, it raises an IOError exception.

but should "file" be the full path to the font,
or just a font name ?

Same question for method truetype:
truetype
ImageFont.truetype(file, size) => Font instance

Load a TrueType or OpenType font file, and create a font object. This
function loads a font object from the given file, and creates a font
object for a font of the given size.
On Windows, if the given file name does not exist, the loader also looks
in Windows fonts directory.


Also, only true type fonts can be resized:
there is no easy way to resize standard fonts ?
(I am not a font expert :) )


Thanks in advance for your answers.


Regards,
 
C

Cousin Stanley

| ....
| Also, are there some real examples of use of fonts in PIL ?
| ....

Stéphanie ....

Following is an example of using the PIL ImageFont class ....

'''
Module ....... pil_ImageFont.py
Code_By ...... Stanley C. Kitching
Code_Date .... 2004-02-09
'''

import Image
import ImageFont
import ImageDraw

this_list = [ " Look, up in the sky ......... " ,
" It's a bird ................ " ,
" It's a plane ............... " ,
" It's S u p e r M a n !!!!! " ,
" " ,
" More powerful" ,
" than a locomotive !!!!!!" ,
" " ,
" Able to leap tall buildings" ,
" in a single bound !!!!!!" ]

this_image = Image.new( 'RGB' , ( 360 , 240 ) )

# Adjust dir_PIL path to YOUR installation

dir_PIL = 'K:/Python23/Lib/site-packages/PIL/'

dir_fonts = dir_PIL + 'pilfonts/'

this_font = 'courB12.pil'

i_font = ImageFont.load( dir_fonts + this_font )

this_col = 10
this_row = 10
row_inc = 20

for this_line in this_list :

i_draw = ImageDraw.Draw( this_image )

i_draw.text( ( this_col , this_row ) , this_line , font = i_font )

this_row += row_inc

this_image.save( 'pil_ImageFont.png' )

this_image.show()
 
S

Stéphane Ninin

Also sprach Cousin Stanley :
| ....
| Also, are there some real examples of use of fonts in PIL ?
| ....

Stéphanie ....

Stéphane please, Stéphanie is a woman's name here. :)


Thanx for the script, I will look at it soon and adapt it for my plot script.

Regards,
 
C

Cousin Stanley

| Stéphane please, Stéphanie is a woman's name here

Stéphane ....

Sorry for the typo ....

Over-clocked fingers NOT synchronized with feeble brain ....

| Thanx for the script, ....

You're welcome ....
 
S

Stéphane Ninin

Also sprach Cousin Stanley :

I had a quick look at it and at the pilfonts...

Just one last question:
am I dreaming or all fonts are in white on back ?
(and cannot be seen if you have a white background) ?

Or is there a way to set the color of text ?

Regards,
 
S

Stéphane Ninin

Also sprach Cousin Stanley :

I had a quick look at it and at the pilfonts...

Just one last question:
am I dreaming or all fonts are in white ?
(and so cannot be seen if you have a white background) ?

Or is there a way to set the color of text ?

Regards,
 
C

Cousin Stanley

| ....
| am I dreaming or all fonts are in white ?
| (and so cannot be seen if you have a white background) ?
|
| Or is there a way to set the color of text ?

Stéphane ....

# Set the background color when creating the image ....

this_image = Image.new( 'RGB' , ( 360 , 240 ) , 'white' )

# Set the fill color when drawing the text ....

i_draw.text( ( this_col , this_row ) ,
this_line ,
font = i_font ,
fill = 'black' )
 
S

Stéphane Ninin

Also sprach Cousin Stanley :
# Set the background color when creating the image ....

this_image = Image.new( 'RGB' , ( 360 , 240 ) , 'white' )

# Set the fill color when drawing the text ....

i_draw.text( ( this_col , this_row ) ,
this_line ,
font = i_font ,
fill = 'black' )

Well, it works just fine, thanks again.
Maybe I have not browsed the documentation long enough,
(the online doc,http://www.pythonware.com/library/pil/handbook/index.htm)
but I didnot see any information
about all possible options that can be used.

Image.new doc says just:

Image.new(mode, size) => image
Image.new(mode, size, color) => image
Creates a new image with the given mode and size. Size is given as a 2-
tuple. The colour is given as a single value for single-band images, and a
tuple for multi-band images (with one value for each band). If the colour
argument is omitted, the image is filled with black. If the colour is None,
the image is not initialised.

and for text in ImageDraw it says:

text
draw.text(position, string, options)
Draws the string at the given position. The position gives the upper right
corner of the text.
The font option is used to specify which font to use. It should be an
instance of the ImageFont class, typically loaded from file using the load
method in the ImageFont module.
The fill option gives the colour to use for the text.


Where did you find all these options ? Are they only in source code ?
Or is there some other doc I am unaware of ?

Regards,
 
C

Cousin Stanley

| Well, it works just fine, thanks again.

You're welcome ....

| Where did you find all these options ?

The PIL documentation I have is in a PDF file
that I think came from the following link ....

http://www.pythonware.com/products/pil/pil-handbook.pdf

I don't know if the PDF version is still there
and if it is, wheter or not if is in sync ( older or newer than )
with HTML version from the link you gave

http://www.pythonware.com/library/pil/handbook/index.htm

Those two sources are the only ones I have ....
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top