In the series of python projects that we are building, we will build a python program that will automatically add a watermark to images.
You will see how we can use the pillow library in python to add watermark to images. This is a prevalent task, so we will automate this process by using python programming.
Add watermark to image in python
To add a watermark to an image in python you can use the pillow library in python. You can follow the following steps to add a watermark to an image.
Step No 1: Install the Required modules
There are two required libraries that you should install in your system. The first one is os module, which is already installed in your system if you have python installed, and the second is the pillow library in python, which you should install.
pip install pillow
Step No 2: Set the position of Image where you want to add the watermark
Once you have installed the required python libraries then you should start writing code and set the points where you want to paste the image. in our case, we have to use the right bottom corner of the image.
position = base_image.size
newsize = (int(position[0]*8/100),int(position[0]*8/100))
Step No 3: Paste the watermarked Image and save the original Image
Now is the time to paste the watermark image to the original image. use the paste()
function to add the watermark image to the original image.
transparent.paste(watermark,new_position,watermark)
Python code to add a watermark to an Image
Run the following python code in your IDE, and you will see the output image with a watermark image.
Have a look at Python Libraries for Photo Editing
import os
from PIL import Image
def watermark_photo(input_image_path,watermark_image_path,output_image_path):
base_image = Image.open(input_image_path)
watermark = Image.open(watermark_image_path).convert("RGBA")
# add watermark to your image
position = base_image.size
newsize = (int(position[0]*8/100),int(position[0]*8/100))
# print(position)
watermark = watermark.resize(newsize)
# print(newsize)
# return watermark
new_position = position[0]-newsize[0]-20,position[1]-newsize[1]-20
# create a new transparent image
transparent = Image.new(mode='RGBA',size=position,color=(0,0,0,0))
# paste the original image
transparent.paste(base_image,(0,0))
# paste the watermark image
transparent.paste(watermark,new_position,watermark)
image_mode = base_image.mode
print(image_mode)
if image_mode == 'RGB':
transparent = transparent.convert(image_mode)
else:
transparent = transparent.convert('P')
transparent.save(output_image_path,optimize=True,quality=100)
print("Saving"+output_image_path+"...")
folder = input("Enter Folder Path:")
watermark = input("Enter Watermark Path:")
os.chdir(folder)
files = os.listdir(os.getcwd())
print(files)
if not os.path.isdir("output"):
os.mkdir("output")
c = 1
for f in files:
if os.path.isfile(os.path.abspath(f)):
if f.endswith(".png") or f.endswith(".jpg"):
watermark_photo(f,watermark,"output/"+f)
Related Python Projects
- Random Password Generator
- Convert a json file to a csv file in python
- Write hello world in Python
- Create Analog Clock with Python Turtle Module
- Create an Alarm Clock With Python - Python Project
- Create an Amazing Art With Python Turtle - Pickachu Character
- How to Create Decimal to Binary Converter with tkinter Python
- Python Turtle Project - Create Doraemon Character
- Python Will Recommend you A Movie Name
- Search a String Inside a File in Python
Summary and Conclusion
In this tutorial, you have seen how we can use the python pillow library to add a watermark to an image. If you have any questions, please leave them in the comment section.