Python Pillow – Creating a Watermark

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:
Text watermark,Image watermark.
Text watermark,
Image 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 libraryfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as np
 
Step 2: Open the image using a photo viewer.
Python3
image = Image.open("puppy.jpg")  # this open the photo viewerimage.show() plt.imshow(image)
Output:
Step 3: Creation of Text watermark
Python
# text Watermarkfrom PIL import ImageFontfrom PIL import ImageDrawwatermark_image = image.copy()  draw = ImageDraw.Draw(watermark_image)font = ImageFont.truetype("arial.ttf", 50)  # add watermarkdraw.text((0, 0), "puppy",           (0, 0, 0), font=font)plt.subplot(1, 2, 1)plt.title("black text")plt.imshow(watermark_image)  # add watermarkdraw.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 librariesfrom PIL import Imagefrom PIL import ImageFontfrom PIL import ImageDrawimport matplotlib.pyplot as pltimport numpy as np  # image openingimage = Image.open("puppy.jpg")# this open the photo viewerimage.show()  plt.imshow(image)  # text Watermarkwatermark_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 textdraw.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 textdraw.text((0, 0), "puppy", (255, 255, 255), font=font)plt.subplot(1, 2, 2)plt.title("white text")plt.imshow(watermark_image)
Output:
Explanation:
Import all the libraries for image processing.Use Image.open() for the opening of image and image.show() for the photo viewer to open.plt.imshow() is used to open the image in the IDE.Make a copy of an image for the creation of watermark image.Make the image editable using ImageDraw.Use ImageFont to specify font and font size.Create a draw method of ImageDraw module and passed the image as a parameter in the function.Create a Font using ImageFont module function truetype() as it needs two parameters that is(“font type”,size)Then used text() function of draw object and passed the 4 parameters(Point of starting for text, “sample text”, Color, ImageFont object).plt.Imshow(watermark_image) for the output.
Import all the libraries for image processing.
Use Image.open() for the opening of image and image.show() for the photo viewer to open.
plt.imshow() is used to open the image in the IDE.
Make a copy of an image for the creation of watermark image.
Make the image editable using ImageDraw.
Use ImageFont to specify font and font size.
Create a draw method of ImageDraw module and passed the image as a parameter in the function.
Create a Font using ImageFont module function truetype() as it needs two parameters that is(“font type”,size)
Then used text() function of draw object and passed the 4 parameters(Point of starting for text, “sample text”, Color, ImageFont object).
plt.Imshow(watermark_image) for the output.
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 libraryfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as np
Step 2: Open the image using a photo viewer
Python3
# read imageimage=Image.open("lion.jpg")image.show() plt.imshow(image)
Step 3: Creation of Image watermark
Python3
# image watermarksize = (500, 100)crop_image = image.copy()crop_image.thumbnail(size)  # add watermarkcopied_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 librariesfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as np  # to open the imageimage = Image.open("lion.jpg")# this open the photo viewerimage.show()plt.imshow(image)  # image watermarksize = (500, 100)crop_image = image.copy()# to keep the aspect ration in intactcrop_image.thumbnail(size)  # add watermarkcopied_image = image.copy()# base imagecopied_image.paste(crop_image, (500, 200))# pasted the crop image onto the base imageplt.imshow(copied_image)
Output:
Step-wise Explanation:
Import all the libraries for image processing.Use open () for the opening of image and show() for the photo viewer to open.plt.imshow() is used to open the image in the IDE.Use copy() module to copy the image in the crop_imageCrop_image.thumbnail(size) -thumbnail is used to keep the aspect ratio intact.copied_image=image1.copy() is used to keep the base for the image onto which we would paste the image.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).plt.imshow() to show the image.
Import all the libraries for image processing.
Use open () for the opening of image and show() for the photo viewer to open.
plt.imshow() is used to open the image in the IDE.
Use copy() module to copy the image in the crop_image
Crop_image.thumbnail(size) -thumbnail is used to keep the aspect ratio intact.
copied_image=image1.copy() is used to keep the base for the image onto which we would paste the image.
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).
plt.imshow() to show the image.
Picked
Python-pil
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.