Welcome folks today in this blog post we will be adding a text watermark to image
in python using pillow
library. All the full source code of the application is given below.
Get Started
In order to get started you need to install pillow
library inside your python project by executing the pip
command which is shown below
pip install pillow
After installing this library just create an app.py
file and copy paste the following code
app.py
from PIL import Image, ImageDraw, ImageFont
#Create an Image Object from an Image
im = Image.open('###pathofimage###')
width, height = im.size
draw = ImageDraw.Draw(im)
text = "###watermark text###"
font = ImageFont.truetype('arial.ttf', 36)
textwidth, textheight = draw.textsize(text, font)
# calculate the x,y coordinates of the text
margin = 10
x = width - textwidth - margin
y = height - textheight - margin
# draw watermark in the bottom right corner
draw.text((x, y), text, font=font)
im.show()
#Save watermarked image
im.save('watermark.jpg')
Here in this block of code we are importing the library pillow
and then loading the input image path so here you need to replace the path of the image and also the watermark
text respectively. After this you just need to run the python
application by running the below command
python app.py
As you can see the image has the watermark text
attached to it at the bottom right position of the image.