Adding Text Watermarks to Images with Python 3 and Pillow
This tutorial explains how to use Python 3 and the Pillow library to add customizable text watermarks to single images and batch‑process entire folders, covering required installations, code implementation, and saving the watermarked results.
In the digital age, protecting images with watermarks is essential, and Python provides a convenient way to automate this task.
Required tools and preparation : Ensure Python 3 is installed and install the Pillow library using pip install Pillow . Place the images you want to watermark in a single folder.
Implementation process
We will use Pillow's Image , ImageDraw , and ImageFont modules. First, define an add_watermark function that takes the image path, watermark text, and save path as parameters.
import os
from PIL import Image, ImageDraw, ImageFont
def add_watermark(img_path, text, save_path):
img = Image.open(img_path)
# rest of the code here
# Open the image
img = Image.open(img_path)Next, add the watermark using ImageDraw . Prepare the watermark text, font, size, and color, then draw the text at the bottom‑right corner of the image.
watermark_text = text
font = ImageFont.truetype('arial.ttf', 36)
text_width, text_height = font.getsize(watermark_text)
draw = ImageDraw.Draw(img)
draw.text((width-text_width-10, height-text_height-10), watermark_text, font=font, fill=(255,255,255,128))Save the watermarked image with img.save(save_path) .
img.save(save_path)To process all images in a directory, define add_watermark_to_all which iterates over files ending with .jpg and calls add_watermark for each.
def add_watermark_to_all(folder_path, text):
for filename in os.listdir(folder_path):
if filename.endswith('.jpg'):
# rest of the code hereFinally, the complete script combines these functions and runs them when the module is executed.
import os
from PIL import Image, ImageDraw, ImageFont
def add_watermark(img_path, text, save_path):
img = Image.open(img_path)
watermark_text = text
width, height = img.size
font = ImageFont.truetype('arial.ttf', 36)
text_width, text_height = font.getsize(watermark_text)
draw = ImageDraw.Draw(img)
draw.text((width-text_width-10, height-text_height-10), watermark_text, font=font, fill=(255,255,255,128))
img.save(save_path)
def add_watermark_to_all(folder_path, text):
for filename in os.listdir(folder_path):
if filename.endswith('.jpg'):
img_path = folder_path + filename
save_path = folder_path + 'marked_' + filename
add_watermark(img_path, text, save_path)
if __name__ == '__main__':
folder_path = '/path/to/folder/'
watermark_text = 'My Watermark'
add_watermark_to_all(folder_path, watermark_text)Conclusion : Using Python 3 and Pillow, you can easily add text watermarks to individual images or batch‑process a whole directory, making Python a powerful platform for both amateur and professional image processing tasks.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.