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
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) w, h = image.size x, y = int (w / 2 ), int (h / 2 ) if x > y: font_size = y elif y > x: font_size = x else : font_size = x font = ImageFont.truetype( "arial.ttf" , int (font_size / 6 )) # add watermark draw.text((x, y), "puppy" , fill = ( 0 , 0 , 0 ), font = font, anchor = 'ms' ) plt.subplot( 1 , 2 , 1 ) plt.title( "black text" ) plt.imshow(watermark_image) # add watermark draw.text((x, y), "puppy" , fill = ( 255 , 255 , 255 ), font = font, anchor = 'ms' ) 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) w, h = image.size x, y = int (w / 2 ), int (h / 2 ) if x > y: font_size = y elif y > x: font_size = x else : font_size = x font = ImageFont.truetype( "arial.ttf" , int (font_size / 6 )) # add Watermark # (0,0,0)-black color text draw.text((x, y), "puppy" , fill = ( 0 , 0 , 0 ), font = font, anchor = 'ms' ) plt.subplot( 1 , 2 , 1 ) plt.title( "black text" ) plt.imshow(watermark_image) # add Watermark # (255,255,255)-White color text draw.text((x, y), "puppy" , fill = ( 255 , 255 , 255 ), font = font, anchor = 'ms' ) 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.
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:
- 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.