Add Watermark to Image Using Python Pillow: A Step Guide – Python Pillow Tutorial

By | August 30, 2020

If you plan to add watermark to an image in python, python pillow library is a good choice. In this tutorial, we will introduce you how to add.

Preliminary

In order to add watermark to an image, you should be sure two main problems:

1.Where to add the watermark in the image.

2.What kind of font, color and font size will be used.

We will fix these two questions one by one and add watermark to an image.

Import library

In this tutorial, we will use python pillow to add watermark to an image, we should import some libraries.

from PIL import Image, ImageDraw, ImageFont

Convert the image to RGBA

This step is very important, we will use alpha channel.

image = Image.open(file_obj).convert('RGBA')

Create a wartmark image based image size

We will compose image and watermark image, which means the size of them should be the same.

txt = Image.new('RGBA', image.size, (255,255,255,0))

Set watermark font type and size

Look at code below:

    draw = ImageDraw.Draw(txt)
    width, height = image.size
    margin = 10
    font = ImageFont.truetype('C:\\windows\\Fonts\\ANTQUAB.TTF', 30)
    textWidth, textHeight = draw.textsize(text, font)

C:\\windows\\Fonts\\ANTQUAB.TTF is the font type, 30 is the font size.

Draw text watermark to image

Here x and start_y is the postion of watermark in the image. We will draw text here.

    x = (width - textWidth - margin) / 2  
    
    sep = int(height/4)
    min_sep = 300
    if sep < min_sep:
        sep = min_sep
    start_y = sep
    
    while start_y < (height - textHeight - margin):
        draw.text((x, start_y), text, font = font, fill=(250, 0, 0,50))
        start_y += start_y

fill=(250, 0, 0,50) is (r, g,b, alpha) . In this tutorial, we will use color red to draw text.

Compose wartermark and the image

    out = Image.alpha_composite(image, txt)

    image.close()
    #out.show()
    
    out.save(file_obj)
    out.close()

Then we can create a python function to add wartermark to an image, this function is:

def watermark_with_text(file_obj, text = 'www.tutorialexample.com'):
    image = Image.open(file_obj).convert('RGBA')
    txt = Image.new('RGBA', image.size, (255,255,255,0))
    
    draw = ImageDraw.Draw(txt)
    width, height = image.size
    margin = 10
    font = ImageFont.truetype('C:\\windows\\Fonts\\ANTQUAB.TTF', 30)
    textWidth, textHeight = draw.textsize(text, font)
    x = (width - textWidth - margin) / 2 
    
    sep = int(height/4)
    min_sep = 300
    if sep < min_sep:
        sep = min_sep
    start_y = sep
    
    while start_y < (height - textHeight - margin):
        draw.text((x, start_y), text, font = font, fill=(250, 0, 0,50))
        start_y += start_y
        
    out = Image.alpha_composite(image, txt)

    image.close()
    #out.show()
    
    out.save(file_obj)
    out.close()
    #image.show()
    return file_obj

Then we can call this function like this:

im = 'vertical.png'
watermark_with_text(im)

Run this code, we will get an image with watermark www.tutorialexample.com.

add wartermark to an image using python pillow

Leave a Reply