Profile picture

Co-founder @ RMOTR

How to Generate Images With Text Using Python and PIL

Last updated: December 7th, 20182018-12-07Project preview

This is a quick guide to show how to draw images with Pillow (recommended PIL's fork). Let's start with a few imports and work the examples step by step:

In [1]:
from PIL import Image, ImageDraw, ImageFont
from utils import display_pillow_image

Drawing a simple square image

We'll use Image.new to generate a new image of w width and h height, and a specific color; I'll draw a red rectangle (100x100):

In [5]:
w = 100 # 100 pixels wide
h = 100 # 100 pixels high

img = Image.new('RGB', (w, h), color='#FF0000')
In [6]:
display_pillow_image(img)
Out[6]:

Color can be specified in hex (#FF0000) or as an RGB array:

In [9]:
img = Image.new('RGB', (350, 70), color=(51, 144, 255))
In [10]:
display_pillow_image(img)
Out[10]:

Adding text to the image

We can create an ImageDraw object to add 2D graphics to the image, for example, text:

In [11]:
img = Image.new('RGB', (350, 70), color=(51, 144, 255))
In [12]:
# create the canvas
canvas = ImageDraw.Draw(img)
In [13]:
canvas.text((10, 10), "Hello World", fill='#FFFFFF')
In [14]:
display_pillow_image(img)
Out[14]:

The first tuple passed to text ((10, 10)) is the x and y position of the text, another example:

In [34]:
img = Image.new('RGB', (350, 70), color=(51, 144, 255))
In [35]:
# create the canvas
canvas = ImageDraw.Draw(img)
In [36]:
canvas.text((250, 25), "Hello World", fill='#FFFFFF')
In [37]:
display_pillow_image(img)
Out[37]:

Customizing the style of text

You can use ImageFont to customize the size and the font used. For example, I've included our main typography (Lato) in this project. I can load it locally and use it for the text:

In [25]:
img = Image.new('RGB', (350, 70), color=(51, 144, 255))
In [26]:
# create the canvas
canvas = ImageDraw.Draw(img)
In [27]:
font = ImageFont.truetype('Lato-Bold.ttf', size=24)
In [28]:
canvas.text((10, 10), "Hello World", font=font, fill='#FFFFFF')
In [29]:
display_pillow_image(img)
Out[29]:

Centering the text

There's a useful method of the ImageDraw object that lets you calculate the width and height of the text that you're about to draw. You can use it to compute the correct x and y position of the text.

In [45]:
image_width = 350
image_height = 70
In [46]:
img = Image.new('RGB', (image_width, image_height), color=(51, 144, 255))
In [47]:
# create the canvas
canvas = ImageDraw.Draw(img)
In [48]:
font = ImageFont.truetype('Lato-Bold.ttf', size=24)
In [49]:
text_width, text_height = canvas.textsize('Hello World', font=font)
In [50]:
print(f"Text width: {text_width}")
print(f"Text height: {text_height}")
Text width: 134
Text height: 24
In [52]:
x_pos = int((image_width - text_width) / 2)
y_pos = int((image_height - text_height) / 2)
In [53]:
print(f"X: {x_pos}")
print(f"Y: {y_pos}")
X: 108
Y: 23
In [54]:
canvas.text((x_pos, y_pos), "Hello World", font=font, fill='#FFFFFF')
In [55]:
display_pillow_image(img)
Out[55]:

That's it! What other examples can you think about?

Notebooks AI
Notebooks AI Profile20060