Drawing Text on Images With Pillow and Python

Drawing Text on Images With Pillow and Python

Learn how to draw, beautify, and customize text on images using Pillow and Python.

Pillow supports drawing text on your images in addition to shapes. Pillow uses its own font file format to store bitmap fonts, limited to 256 characters. Pillow also supports TrueType and OpenType fonts as well as other font formats supported by the FreeType library.

In this chapter, you will learn about the following:

  • Drawing Text
  • Loading TrueType Fonts
  • Changing Text Color
  • Drawing Multiple Lines of Text
  • Aligning Text
  • Changing Text Opacity
  • Learning About Text Anchors

While this article is not completely exhaustive in its coverage of drawing text with Pillow, when you have finished reading it, you will have a good understanding of how text drawing works and be able to draw text on your own.

Let's get started by learning how to draw text.

Drawing Text

Drawing text with Pillow is similar to drawing shapes. However, drawing text has the added complexity of needing to be able to handle fonts, spacing, alignment, and more. You can get an idea of the complexity of drawing text by taking a look at the text() function's signature:

This function takes in a lot more parameters than any of the shapes you can draw with Pillow! Let's go over each of these parameters in turn:

  • xy - The anchor coordinates for the text (i.e. where to start drawing the text).
  • text - The string of text that you wish to draw.
  • fill - The color of the text (can a tuple, an integer (0-255) or one of the supported color names).
  • font - An ImageFont instance.
  • anchor - The text anchor alignment. Determines the relative location of the anchor to the text. The default alignment is top left.
  • spacing - If the text is passed on to multiline_text(), this controls the number of pixels between lines.
  • align - If the text is passed on to multiline_text()"left""center" or "right". Determines the relative alignment of lines. Use the anchor parameter to specify the alignment to xy.
  • direction - Direction of the text. It can be "rtl" (right to left), "ltr" (left to right) or "ttb" (top to bottom). Requires libraqm.
  • features - A list of OpenType font features to be used during text layout. Requires libraqm.
  • language - The language of the text. Different languages may use different glyph shapes or ligatures. This parameter tells the font which language the text is in, and to apply the correct substitutions as appropriate, if available. It should be a BCP 47 language code. Requires libraqm.
  • stroke_width - The width of the text stroke
  • stroke_fill - The color of the text stroke. If you don't set this, it defaults to the fill parameter's value.
  • embedded_color - Whether to use font embedded color glyphs (COLR or CBDT).

python web dev images text pillow

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Convert Text to Speech in Python

Learn how to convert your Text into Voice with Python and Google APIs. Text to speech is a process to convert any text into voice. Text to speech project takes words on digital devices and convert them into audio with a button click or finger touch.

A Beginner's Guide to Web Scraping in Python

In this article, you're going to learn the basics of web scraping in python and we'll do a demo project to scrape quotes from a website.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.