Python Pillow: Creating Text on Image And Solid Colors

Pillow is a fork of the python Imaging library , PIL.

PIL is an imaging library created to make and manipulate image using the python programming language.

Some might already be familiar with PIL which is the original library.

Don’t get it twisted.

Like i said earlier, Pillow is a fork of PIL. PIL is no more being maintained and hasn’t been updated since 2009.

Moreover, PIL is specifically designed for python 2 which is now obsolete

We are all using python 3 now so we ‘re to use pillow.

In summary, Pillow is a repackaged version of PIL.

So now i have cleared the difference between PIL and Pillow, let’s go on to installing it.

Installing Pillow

You should install pillow on your system using PIP

 pip install Pillow 

After installing Pillow, i have to point out that even though you installed pillow using with the name “Pillow”, you will still have to import it using PIL because of backward compatibility

Writing Text on Solid Color

To use Pillow, we always have to either create an image or load an already existing ones.

Even solid color are image , it’s just that pictures makes sense to us as image than solid colors But it’s all being considered an image to computers.

So before we write text on solid color, we firstly need to create an image of the solid color itself.

You should see the below code snippet on how to do this.

# import the require module classes from PIL
From PIL import Image, ImageDraw
# we need to create an image object
image = Image.new('RGB', (200,200), 'blue')
# we create a ImageDraw object on our image
drawer = ImageDraw.Draw(image)
# write a text on the image 
drawer.text((10,10), 'Hello Geekypage Fans')
# save our image to file 
image.save('blueimagetext.jpg')

So, How does the above snippet works ?

Firstly, I imported the Image and the ImageDraw from the PIL.

image = Image.new('RGB', (200,200), 'blue')

Image is for creating a image while ImageDraw is for drawing the text on the image.

Then i created an RGB image solid color object. width of 200 and height of 200 with a blue background.

# we create a ImageDraw object on our image
drawer = ImageDraw.Draw(image)
# write a text on the image 
drawer.text((10,10), 'Hello Geekypage Fans')

After that i created an ImageDraw object for our previously created object and draw the text ‘Hello Geekypage Fans’ at position 10,10 (x,y).

# save our image to file 
image.save('blueimagetext.jpg')

Finally, I save the image to a file named ‘blueimagetext.jpg’. If this file already exists, it will be overwritten.

This is what my output image ”blueimagetext.jpg’ looks like

python pillow text on image

You can see we’re importing from PIL even though i installed Pillow. Like i said earlier, this is for backward compatibility.

You should note this.

Writing Text on Image

This time i am not creating a solid color image like the blue background i did earlier. I will be using an already existing image or picture.

For example, a picture of a tree or forest or a picture of an animal say a tortoise.

Yes, let’s use a picture of a tortoise and we going to make a script that will write “This is a tortoise” on the picture for us.

The code will be similar to the solid color one except that we won’t be creating a solid color, instead i’m going to load an image from my computer and use the ImagDraw class to draw the text on it.

See below:

# import the require module classes from PIL
From PIL import Image, ImageDraw
# we don't need to create an image like we did earlier
# this time we going to load an already existing one
image = Image.open('tortoise.jpg')
# we create a ImageDraw object on our image
drawer = ImageDraw.Draw(image)
# write a text on the image 
drawer.text((10,10), 'This is a Tortoise')
# save our image to file 
image.save('blueimagetext.jpg')

The code is the same thing look closely. we’re only loading a picture on my computer but before we’re creating a solid color image.

You can play around with the position of the text drawn on the image. I used 10,10 which means position x at 10 and y at 10. Play around with it and see what suits your need.

Here’s the result of our image

Styling the text with font

We can also style the font of the text written on the image. I don’t particularly like the default text.

It looks kinda ugly.

You are going to need a font file for this. In my case i downloaded the arial truetype font.

There are lot of place you can download ttf font on the internet.

To add the font to our code. We need to import one more module class named ImageFont

See the code below

# import the require module classes from PIL
From PIL import Image, ImageDraw, ImageFont
# we don't need to create an image like we did earlier
# this time we going to load an already existing one
image = Image.open('tortoise.jpg')
# we create a ImageDraw object on our image
drawer = ImageDraw.Draw(image)
# the the font object
font = ImageFont.truetype('Arial.ttf', 14)
# write a text on the image 
drawer.text((10,10), 'This is a Tortoise', font=font, fill=(225,0,0))
# save our image to file 
image.save('blueimagetext.jpg')

The ImageFont.truetype is use to specify the font file and

We passed two additional parameter to the text method font and fill.

The font parameter specified the font object we loaded with the ImageFont.truetype and fill is the color of the text .

The color is passed in with a tuple of 3 color representing RGB.

Here’s our result.

And that’s it.

If you got any comment or contribution, please drop a comment below.

Leave a Reply

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh