Python PIL ImageFont.load_default()

Python PIL ImageFont.load_default()

PIL is the Python Imaging Library, which provides image editing capabilities for the Python interpreter.
The ImageFont module defines a class of 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 tool to convert BDF and PCF font descriptors (X Window Font Format) to this format.

As of version 1.1.4, PIL can be configured to support TrueType and OpenType fonts (as well as other font formats supported by the FreeType library). For earlier versions, TrueType support was only available as part of the imToolkit package.

ImageFont.load_default() loads a default font that says “eat healthy, live healthy.”

Syntax: ImageFont.load_default()

Parameters:
text – Writes the text to load.

Returns: A font object.

from PIL import Image, ImageFont, ImageDraw
  
text = "eat healthy live healthy"
font = ImageFont.load_default()
im = Image.new("L", font.getsize(text), 255)
  
# document
dctx = ImageDraw.Draw(im)
dctx.text((0, 0), text, font = font)
deldctx
im = im.resize((im.width * 6, im.height * 8))
  
# img is saved as specified
im.save("geeks3.png")

Output:
Python PIL ImageFont.load_default()

Another example: This changes the text and loads a default font that’s better than nothing.

from PIL import Image, ImageFont, ImageDraw
  
text = "better than nothing"
font = ImageFont.load_default()
im = Image.new("L", font.getsize(text), 255)
  
# document
dctx = ImageDraw.Draw(im)
dctx.text((0, 0), text, font = font)
deldctx
im = im.resize((im.width * 6, im.height * 6))
  
# img is saved as specified
im.save("geeks2.png")

Output:
Python PIL ImageFont.load_default()

Leave a Reply

Your email address will not be published. Required fields are marked *