In this blog post, we will be learning how to add watermarks to images using Python. We will be using the pillow library which is a fork of the PIL library and is used for image manipulation in Python. This is a great library that allows us to do many different things with images including adding watermarks.
First, we need to install the pillow library if we do not already have it. We can do this by running the pip install command in our terminal.
pip install pillow
Once the installation is complete, we can import the Image and ImageDraw modules from the pillow library. These modules will allow us to create images and manipulate them.
from PIL import Image, ImageDraw, ImageFont
Next, we need to open the image to that we want to add a watermark to. We can do this by using the Image.open() function. This function takes in the file path of the image as an argument and returns an Image object.
Now, we need to create a watermark. For this, we will use the ImageDraw module’s Draw() function. This function takes in an Image object as an argument and returns a Draw object. We can use this Draw object to draw on our image.
We will use the Draw object’s text() function to add text to our image. This function takes in four arguments:
- xy – The x and y coordinates of where we want our text;
- fill – The color of our text;
- font – A tuple containing the font family and font size;
- and text – The string of text that we want to draw on our image.
Once we have added our watermark, we need to save our image. We can do this by using the Image’s save() function.
This function takes in two arguments:
- filename – The name of the file that we want to save our image as;
- and format – The format that we want to save our file in (ex. “png”).
This is a python script to create a watermark (Source code)
Code language: Python (python)
from PIL import Image, ImageDraw, ImageFont # Create an Image Object from an Image input_image = Image.open("source.jpg") width, height = input_image.size draw = ImageDraw.Draw(input_image) watermark_text = input("Enter your text for watermark \t") font = ImageFont.truetype(font="./font.ttf", size=100) textwidth, textheight = draw.textsize(watermark_text, font) # calculate the x,y coordinates of the text margin = 70 x = width - textwidth - margin y = height - textheight - margin # draw watermark in the bottom right corner draw.text((x, y), watermark_text, font=font, fill=(252, 227, 71)) input_image.show() # Save watermarked image input_image.save("./output_image_with_watermark.jpg")
Install the packages listed in requirements.txt using pip
Code language: CSS (css)
pip install -r requirements.txt
Code language: CSS (css)
cd image_watermark python image_watermark.py
And that’s it! We have now successfully added a watermark to our image file using Python.
In conclusion, adding a watermark to an image file in Python is a relatively simple process that can be accomplished using the pillow library. First, you need to install a pillow if you don’t already have it. Next, you need to open your image and create a watermark with the ImageDraw module’s Draw() function. Finally, you need to save your newly created watermarked image file.