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:
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 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).
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
"right". Determines the relative alignment of lines. Use the anchor parameter to specify the alignment to
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
embedded_color- Whether to use font embedded color glyphs (COLR or CBDT).
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.
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.
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.
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).
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.