Python PIL | ImageFont.truetype()

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.Draw.text() method.

PIL uses its own font file format to store bitmap fonts. You can use the :command`pilfont` utility to convert BDF and PCF font descriptors (X window font formats) to this format.

PIL.ImageFont.truetype() 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.

This function requires the _imagingft service.

Syntax: PIL.ImageFont.truetype(font=None, size=10, index=0, encoding=”)

Parameters:

font – A truetype font file. Under Windows, if the file is not found in this filename, the loader also looks in Windows fonts/ directory.
size – The requested size, in points.
index – Which font face to load (default is first available face).
encoding – Which font encoding to use (default is Unicode).

Returns: A font object.
Exception: IOError – If the file could not be read.

Image Used:

filter_none

edit
close

play_arrow

link
brightness_4
code

   
# Importing Image and ImageFont, ImageDraw module from PIL package 
from PIL import Image, ImageFont, ImageDraw
      
# creating a image object
image = Image.open(r'C:\Users\System-Pc\Desktop\rose.jpeg'
  
draw = ImageDraw.Draw(image)
  
font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 70)
  
text = 'DO NOT DRINK AND \nDRIVE'
  
draw.text((10, 20), text, font = font)
  
image.show()

chevron_right


Output:

Another example:Take another image.
Image Used

filter_none

edit
close

play_arrow

link
brightness_4
code

Importing Image and ImageFont, ImageDraw module from PIL package 
from PIL import Image, ImageFont, ImageDraw
      
# creating a image object
image = Image.open(r'C:\Users\System-Pc\Desktop\flower.jpg'
  
draw = ImageDraw.Draw(image)
  
font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 70)
  
text = 'stay healthy'
  
draw.text((50, 100), text, font = font)
  
image.show()

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.