Home United States USA — software Drawing Text on Images With Pillow and Python

Drawing Text on Images With Pillow and Python

253
0
SHARE

Learn how to draw, beautify, and customize text on images using Pillow and Python.
Join the DZone community and get the full member experience. 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: You probably won’t use most of these parameters are on a regular basis unless your job requires you to work with foreign languages or arcane font features. When it comes to learning something new, it’s always good to start with a nice example. Open up your Python editor and create a new file named draw_text.py. Then add this code to it: Here you create a small image using Pillow’s Image.new() method. It has a nice green background. Then you create a drawing object. Next, you tell Pillow where to draw the text. In this case, you draw two lines of text. When you run this code, you will get the following image: That looks pretty good. Normally, when you are drawing text on an image, you would specify a font. If you don’t have a font handy, you can use the method above or you can use Pillow’s default font. Here is an example that updates the previous example to use Pillow’s default font: n this version of the code, you use ImageFont.load_default() to load up Pillow’s default font. Then you apply the font to the text, you pass it in with the font parameter. The output of this code will be the same as the first example. Now let’s discover how to use a TrueType font with Pillow! Pillow supports loading TrueType and OpenType fonts. So if you have a favorite font or a company mandated one, Pillow can probably load it. There are many open source TrueType fonts that you can download. One popular option is Gidole, which you can get here: The Pillow package also comes with several fonts in its test folder. You can download Pillow’s source here: This book’s code repository on Github includes the Gidole font as well as a handful of the fonts from the Pillow tests folder that you can use for the examples in this chapter: To see how you can load up a TrueType font, create a new file and name it draw_truetype.py. Then enter the following: For this example, you use the Gidole font and load an image taken at the Dallas Arboretum in Texas: Then you loop over several different font sizes and write out a string at different positions on the image. When you run this code, you will create an image that looks like this: That code demonstrated how to change font sizes using a TrueType font. Now you’re ready to learn how to switch between different TrueType fonts. Create another new file and name this one draw_multiple_truetype.py. Then put this code into it: Here you use Python’s glob module to search for files with the extension.ttf. Then you loop over those files and write out the font name on the image using each of the fonts that glob found. When you run this code, your new image will look like this: This demonstrates writing text with multiple formats in a single code example.

Continue reading...