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</code><code>from PIL import Image, ImageDraw, ImageFont</code><code>def add_watermark(img_path, text, save_path):</code><code> img = Image.open(img_path)</code><code> # rest of the code here</code><code># Open the image</code><code>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</code><code>font = ImageFont.truetype('arial.ttf', 36)</code><code>text_width, text_height = font.getsize(watermark_text)</code><code>draw = ImageDraw.Draw(img)</code><code>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):</code><code> for filename in os.listdir(folder_path):</code><code> if filename.endswith('.jpg'):</code><code> # rest of the code hereFinally, the complete script combines these functions and runs them when the module is executed.
import os</code><code>from PIL import Image, ImageDraw, ImageFont</code><code>def add_watermark(img_path, text, save_path):</code><code> img = Image.open(img_path)</code><code> watermark_text = text</code><code> width, height = img.size</code><code> font = ImageFont.truetype('arial.ttf', 36)</code><code> text_width, text_height = font.getsize(watermark_text)</code><code> draw = ImageDraw.Draw(img)</code><code> draw.text((width-text_width-10, height-text_height-10), watermark_text, font=font, fill=(255,255,255,128))</code><code> img.save(save_path)</code><code>def add_watermark_to_all(folder_path, text):</code><code> for filename in os.listdir(folder_path):</code><code> if filename.endswith('.jpg'):</code><code> img_path = folder_path + filename</code><code> save_path = folder_path + 'marked_' + filename</code><code> add_watermark(img_path, text, save_path)</code><code>if __name__ == '__main__':</code><code> folder_path = '/path/to/folder/'</code><code> watermark_text = 'My Watermark'</code><code> 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
