By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

fonts in PIL ?

P: n/a

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,

--
Stephane Ninin



Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Stéphane Ninin <st*****@alussinan.org> pisze:
All is ok, except that there seems to be no font coming with PIL.
Where could I fond some ?


http://effbot.org -- look for pilfonts in download section.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #2

P: n/a
Also sprach Jarek Zgoda :
All is ok, except that there seems to be no font coming with PIL.
Where could I fond some ?


http://effbot.org -- look for pilfonts in download section.


Thanks a lot.
Seems there is enough choice so that I wont even need to resize text.
Jul 18 '05 #3

P: n/a
| ....
| 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()

--
Cousin Stanley
Human Being
Phoenix, Arizona

Jul 18 '05 #4

P: n/a
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,

--
Stéphane
Jul 18 '05 #5

P: n/a
| 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 ....

--
Cousin Stanley
Human Being
Phoenix, Arizona

Jul 18 '05 #6

P: n/a
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,
--
Stephane
Jul 18 '05 #7

P: n/a
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,

--
Stéphane Ninin

Jul 18 '05 #8

P: n/a
| ....
| 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' )

--
Cousin Stanley
Human Being
Phoenix, Arizona

Jul 18 '05 #9

P: n/a
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/pi...book/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,

--
Stéphane Ninin



Jul 18 '05 #10

P: n/a
| 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/pi...book/index.htm

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

--
Cousin Stanley
Human Being
Phoenix, Arizona

Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.