Skip to content
Related Articles

Related Articles

Python Pillow – Creating a Watermark
  • Difficulty Level : Basic
  • Last Updated : 05 Apr, 2021

In this article, we will see how the creation of a watermark is done using the Pillow library in Python. Pillow is a Python Image Library(PIL) that is used for manipulating images and working on the different formats of images like (‘jpeg’, ‘png’, ‘gif’, ‘tiff’ etc.). There are various image processing you can do using pillow library like resizing, creating watermarks, rotation of the image, merging various images, blurring the image, etc. PIL displays the image in a photo viewer.

Installation:

To install the library, run the below command in your command prompt.

python -m pip install pip

or

python -m pip install pillow

If pip and pillow are already installed in your device, the system would mention “Requirement already satisfied:”



There are two types of watermark:

  1. Text watermark,
  2. Image watermark.

Text Watermark

It is an approach for text documentation copyright protection. In an image, we can place some simple custom text on images in different fonts and formats.

Step 1: Import all the libraries

Python3




# importing the library
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

 

Step 2: Open the image using a photo viewer.

Python3






image = Image.open("puppy.jpg")
  
# this open the photo viewer
image.show() 
plt.imshow(image)

Output:

Step 3: Creation of Text watermark

Python




# text Watermark
from PIL import ImageFont
from PIL import ImageDraw
watermark_image = image.copy()
  
draw = ImageDraw.Draw(watermark_image)
font = ImageFont.truetype("arial.ttf", 50)
  
# add watermark
draw.text((0, 0), "puppy"
          (0, 0, 0), font=font)
plt.subplot(1, 2, 1)
plt.title("black text")
plt.imshow(watermark_image)
  
# add watermark
draw.text((0, 0), "puppy"
          (255, 255, 255), font=font)
plt.subplot(1, 2, 2)
plt.title("white text")
plt.imshow(watermark_image)

Output:

Below is the complete program based on the above approach:

Python3




# import all the libraries
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import matplotlib.pyplot as plt
import numpy as np
  
# image opening
image = Image.open("puppy.jpg")
# this open the photo viewer
image.show()  
plt.imshow(image)
  
# text Watermark
watermark_image = image.copy()
  
draw = ImageDraw.Draw(watermark_image)
# ("font type",font size)
font = ImageFont.truetype("arial.ttf", 50)
  
# add Watermark
# (0,0,0)-black color text
draw.text((0, 0), "puppy", (0, 0, 0), font=font)
plt.subplot(1, 2, 1)
plt.title("black text")
plt.imshow(watermark_image)
  
# add Watermark
# (255,255,255)-White color text
draw.text((0, 0), "puppy", (255, 255, 255), font=font)
plt.subplot(1, 2, 2)
plt.title("white text")
plt.imshow(watermark_image)

Output:

Explanation:

  1. Import all the libraries for image processing.
  2. Use Image.open() for the opening of image and image.show() for the photo viewer to open.
  3. plt.imshow() is used to open the image in the IDE.
  4. Make a copy of an image for the creation of watermark image.
  5. Make the image editable using ImageDraw.
  6. Use ImageFont to specify font and font size.
  7. Create a draw method of ImageDraw module and passed the image as a parameter in the function.
  8. Create a Font using ImageFont module function truetype() as it needs two parameters that is(“font type”,size)
  9. Then used text() function of draw object and passed the 4 parameters(Point of starting for text, “sample text”, Color, ImageFont object).
  10. plt.Imshow(watermark_image) for the output.

Image Watermark

It is a method used to paste an image onto another image. It takes two parameters one the image is to be pasted and the other where the image is to be pasted.

Step1: Import the libraries 

Python




# importing the library
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

Step 2: Open the image using a photo viewer

Python3




# read image
image=Image.open("lion.jpg")
image.show() 
plt.imshow(image)

Step 3: Creation of Image watermark

Python3




# image watermark
size = (500, 100)
crop_image = image.copy()
crop_image.thumbnail(size)
  
# add watermark
copied_image = image.copy()
copied_image.paste(crop_image, (500, 200))
plt.imshow(copied_image)

Below is the complete program based on the above approach:

Python3




# import all the libraries
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
  
# to open the image
image = Image.open("lion.jpg")
# this open the photo viewer
image.show()
plt.imshow(image)
  
# image watermark
size = (500, 100)
crop_image = image.copy()
# to keep the aspect ration in intact
crop_image.thumbnail(size)
  
# add watermark
copied_image = image.copy()
# base image
copied_image.paste(crop_image, (500, 200))
# pasted the crop image onto the base image
plt.imshow(copied_image)

Output:

Step-wise Explanation:

  1. Import all the libraries for image processing.
  2. Use open () for the opening of image and show() for the photo viewer to open.
  3. plt.imshow() is used to open the image in the IDE.
  4. Use copy() module to copy the image in the crop_image
  5. Crop_image.thumbnail(size) -thumbnail is used to keep the aspect ratio intact.
  6. copied_image=image1.copy() is used to keep the base for the image onto which we would paste the image.
  7. paste() is used to paste the image into the copied_image the 2 parameters used are the (crop_image, and the position to be pasted).
  8. plt.imshow() to show the image.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :