


Text on image how to#
Now let's discover how to use a TrueType font with Pillow! Loading TrueType Fonts
Text on image code#
The output of this code will be the same as the first example. Then you apply the font to the text, you pass it in with the font parameter. N this version of the code, you use ImageFont.load_default() to load up Pillow's default font. Here is an example that updates the previous example to use Pillow's default font: # draw_text_default_font.pyĭraw.text((10, 10), "Hello from", font=font) If you don't have a font handy, you can use the method above or you can use Pillow's default font. Normally, when you are drawing text on an image, you would specify a font. When you run this code, you will get the following image: In this case, you draw two lines of text. Next, you tell Pillow where to draw the text. Here you create a small image using Pillow's Image.new() method. Then add this code to it: # draw_text.pyįrom PIL import Image, ImageDraw, ImageFont Open up your Python editor and create a new file named draw_text.py. When it comes to learning something new, it's always good to start with a nice example. 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. embedded_color - Whether to use font embedded color glyphs (COLR or CBDT).If you don't set this, it defaults to the fill parameter's value. stroke_fill - The color of the text stroke.stroke_width - The width of the text stroke.This parameter tells the font which language the text is in, and to apply the correct substitutions as appropriate, if available. Different languages may use different glyph shapes or ligatures. features - A list of OpenType font features to be used during text layout.It can be "rtl" (right to left), "ltr" (left to right) or "ttb" (top to bottom). Use the anchor parameter to specify the alignment to xy. Determines the relative alignment of lines. align - If the text is passed on to multiline_text(), "left", "center" or "right".spacing - If the text is passed on to multiline_text(), this controls the number of pixels between lines.Determines the relative location of the anchor to the text. fill - The color of the text (can a tuple, an integer (0-255) or one of the supported color names).text - The string of text that you wish to draw.xy - The anchor coordinates for the text (i.e.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 can get an idea of the complexity of drawing text by taking a look at the text() function's signature:ĭef text( xy, text, fill = None, font = None, anchor = None, spacing = 4, align = 'left', direction = None,įeatures = None, language = None, stroke_width = 0, stroke_fill = None, embedded_color = False) However, drawing text has the added complexity of needing to be able to handle fonts, spacing, alignment, and more. Drawing Textĭrawing text with Pillow is similar to drawing shapes. Let's get started by learning how to draw text. 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. In this chapter, you will learn about the following: Pillow also supports TrueType and OpenType fonts as well as other font formats supported by the FreeType library. Pillow uses its own font file format to store bitmap fonts, limited to 256 characters. Pillow supports drawing text on your images in addition to shapes.
