Last modified: Oct 19, 2024 By Alexander Williams

Adding Text and Watermarks to Images using Python Pillow

The Pillow library in Python makes it easy to add text and watermarks to images. This can be useful for branding, adding captions, or creating digital signatures on your photos. In this article, we will learn how to add text and watermarks using Pillow.

Getting Started with Pillow

If you haven't already set up Pillow, follow our guide on Pillow: Installation and getting started (Python) to get started with this library.

Loading an Image

Before adding text or a watermark, you need to load the image using the Image.open() method:


from PIL import Image, ImageDraw, ImageFont

# Load an image
image = Image.open('path_to_your_image.jpg')

Adding Text to an Image

To add text to an image, you can use the ImageDraw.Draw() method, which allows you to draw shapes or text on the image. You'll also need a font, which can be loaded using the ImageFont.truetype() method:


# Create a drawing object
draw = ImageDraw.Draw(image)

# Load a font
font = ImageFont.truetype('arial.ttf', size=36)

# Add text to the image
draw.text((50, 50), "Sample Text", fill="white", font=font)

This code places "Sample Text" at the coordinates (50, 50) with a font size of 36. Adjust the position and text content as needed. For more font options, you can explore different TTF fonts available on your system.

Adding a Watermark

A watermark is typically semi-transparent text or an image overlay. You can achieve this by setting the opacity of the watermark:


# Create a new image with the same size for the watermark
watermark = Image.new('RGBA', image.size, (0, 0, 0, 0))
watermark_draw = ImageDraw.Draw(watermark)

# Add text to the watermark
watermark_draw.text((50, 50), "Watermark", fill=(255, 255, 255, 100), font=font)

# Combine the original image with the watermark
watermarked_image = Image.alpha_composite(image.convert('RGBA'), watermark)

This code creates a semi-transparent watermark with 100 opacity. Adjust the fill parameter for different transparency levels.

Saving the Image with Text or Watermark

After adding text or a watermark, you can save the modified image using the save() method:


# Save the image with text
image.save('image_with_text.jpg')

# Save the watermarked image
watermarked_image.save('watermarked_image.png')

Displaying the Result

To display the updated image directly, use the show() method:


# Display the image with text
image.show()

# Display the watermarked image
watermarked_image.show()

Check out our guide on Python-Pillow: How to Show an image in Python for more details.

Conclusion

Adding text and watermarks using Pillow allows you to personalize your images and protect your content with ease. By following the steps above, you can draw text or overlay watermarks on images efficiently. For more image manipulation techniques, see our articles on Resizing Images Using Python Pillow, Cropping Images Using Python Pillow, and Image Rotation and Flipping with Python Pillow.

For further reading on fonts and text positioning, you may find the official Pillow documentation helpful.