Skip to content
Advertisement

Center-/middle-align text with PIL?

How would I center-align (and middle-vertical-align) text when using PIL?

Advertisement

Answer

Deprecation Warning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.

Code using textbbox instead of textsize.

JavaScript
JavaScript

Result

Result


Use Draw.textsize method to calculate text size and re-calculate position accordingly.

Here is an example:

JavaScript

and the result:

image with centered text

If your fontsize is different, include the font like this:

JavaScript
Advertisement