How to add text with an outline effect to an image using Pillow?

Sep 24, 2025

Leave a message

Michael Brown
Michael Brown
Michael is in charge of the medical equipment factory established in 2018. He has rich knowledge in medical device production and is committed to expanding the company's product line in the medical field.

Hey there! As a Pillow supplier, I'm super stoked to share with you how to add text with an outline effect to an image using Pillow. It's a pretty cool technique that can make your images stand out, whether you're working on a personal project or a professional design.

First off, let's talk a bit about Pillow. Pillow is an amazing Python library that's a fork of the Python Imaging Library (PIL). It's got a ton of features for opening, manipulating, and saving different image file formats. And if you're looking for some great Pillow products, you can check out Pillow.

Now, let's get into the nitty - gritty of adding text with an outline effect.

Prerequisites

Before we start, you need to have Pillow installed. If you haven't installed it yet, you can do so using pip. Just open up your command prompt or terminal and run the following command:

pip install pillow

You'll also need to have a basic understanding of Python programming.

Step 1: Importing the necessary libraries

The first thing we need to do in our Python script is import the Pillow library. Here's how you can do it:

from PIL import Image, ImageDraw, ImageFont

The Image module is used to open and manipulate images. ImageDraw allows us to draw on the image, like adding text and shapes. And ImageFont is used to specify the font for our text.

Step 2: Opening the image

Let's say you have an image file named example.jpg in the same directory as your Python script. You can open it like this:

image = Image.open('example.jpg')
draw = ImageDraw.Draw(image)

The Image.open() function opens the image file, and ImageDraw.Draw() creates a drawing context on the image.

Step 3: Defining the text and font

Now, we need to define the text we want to add and the font we'll use. You can choose any font you have installed on your system. For example, if you want to use Arial and the text is "Hello, World!":

text = "Hello, World!"
font = ImageFont.truetype('arial.ttf', 36)

The truetype() function takes the font file name and the font size as parameters.

Step 4: Adding the outline effect

To add an outline effect to the text, we'll draw the text multiple times with a small offset. First, we'll draw the outline, and then we'll draw the main text on top.

outline_color = (0, 0, 0)  # Black color for the outline
text_color = (255, 255, 255)  # White color for the text
outline_width = 2

# Draw the outline
for x_offset in range(-outline_width, outline_width + 1):
    for y_offset in range(-outline_width, outline_width + 1):
        if x_offset != 0 or y_offset != 0:
            draw.text((100 + x_offset, 100 + y_offset), text, font = font, fill = outline_color)

# Draw the main text
draw.text((100, 100), text, font = font, fill = text_color)

In the above code, we first define the colors for the outline and the text. Then, we use nested for loops to draw the text with a small offset in both the x and y directions to create the outline effect. Finally, we draw the main text on top of the outline.

Step 5: Saving the image

After we've added the text with the outline effect, we need to save the image. You can save it in a different format if you want.

image.save('output.jpg')

That's it! You've successfully added text with an outline effect to an image using Pillow.

Tips and Tricks

  • Experiment with different fonts and colors: You can make your image look really unique by trying out different fonts and color combinations for the text and the outline.
  • Adjust the outline width: Depending on the size of your image and the text, you may need to adjust the outline width to get the best effect.

Related products

If you're into stamping molds and related products, you might also be interested in Fisheye Terminals and Busbar U V W. These products have their own unique features and can be really useful in various industries.

Contact for procurement

If you're interested in purchasing Pillow products or have any questions about the techniques we've discussed, feel free to reach out to us. We're always happy to help and have a great team ready to assist you with your procurement needs.

17335628266035ae5c7f6eee4dfcf04402db756b1aecf

References

  • Pillow official documentation
  • Python programming tutorials

So, go ahead and give it a try. Have fun adding text with outline effects to your images, and if you run into any issues, don't hesitate to ask.

Send Inquiry