Sunday, November 17, 2024
Google search engine
HomeLanguagesPython PIL | ImageFont.truetype()

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:




   
# 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()


Output:

Another example:Take another image.
Image Used




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()


Output:

RELATED ARTICLES

Most Popular

Recent Comments