Text watermark on an image in Python using PIL library
In this program, we gonna learn how to make text watermark on an image in python using PIL library.
You can also refer to this simple watermark tutorial: Watermark image using opencv in python
How to use PIL library for text watermark on an image?.
For the draw text watermark on an image, we have to use the PIL library. In PIL library there is various method but we are going to use a few of them like image open method, show method, image save method and image paste method.
now 1st we have to import PIL library:
#import PIL library to use image relared functionality. from PIL import Image from PIL import ImageDraw from PIL import ImageFont
Create a function which takes some parameters and perform some operations:
#Create a function to perform all operation def picture_watermark(path_of_input_image,path_of_output_image,Text,position):
now we going to use all in-built function of PIL library:
#Image.open function used to open the image Image1 = Image.open(path_of_input_image) Draw=ImageDraw.Draw(Image1) #text color black(3,8,12) font=ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf",40) drawing.text(position,Text,fill=black,font=font) #show() inbuilt function is used to display the image Image1.show() Image1.save(path_of_output_image)
At last call the picture_watermark function by passing all required parameters:
if _name_ == '_main_': #Take path of image image1 = 'lighthouse.jpg' #call picture_watermark function by passing 4 parameters. picture_watermark(image1, 'newimage.jpg','www.mousevspython.com', position=(0,0))
now combine the whole program we get:
#import PIL library to use image relared functionality. from PIL import Image from PIL import ImageDraw from PIL import ImageFont #Create a function to perform all operation def picture_watermark(path_of_input_image,path_of_output_image,Text,position): #Image.open finction used to open the image Image1 = Image.open(path_of_input_image) print("original image ") Draw=ImageDraw.Draw(Image1) #text color black(3,8,12) font=ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf",40) drawing.text(position,Watermark_Text,fill=black,font=font) #show() inbuilt function is used to display the image Image1.show() Image1.save(path_of_output_image) if _name_ == '_main_': #Take path of image image1 = 'lighthouse.jpg' #call picture_watermark function by passing 4 parameters. picture_watermark(image1, 'newimage.jpg','www.mousevspython.com', position=(0,0)) print("text watermarked image ........")
Output:
original image:

Original image
text watermarked image ……..

watermarked with text
I hope you have understood how to add text watermark to an image in Python with PIL library
Also read,
black is not defined (line 12)
drawing is not defined (line 14)