Watermark on Images


Youtube Live session on Tkinter

Adding text on image as watermark

Watermark using Text
from PIL import Image, ImageDraw,ImageFont

path = "E:\\testing\\images\\certificate.png"  # blank certificate
path2 = "E:\\testing\\images\\certificate_1.png"# Path to save

img = Image.open(path, mode="r")

fnt = ImageFont.truetype("times.ttf", 110) # family with size
d1 = ImageDraw.Draw(img)
d1.text((650, 990), "plus2net", fill=(94, 61, 43),font=fnt) # adding text

img.show() # display 
img.save(path2) # save in given path 
Watermark on Images using Pillow library


We can set the location of adding text as watermark based on width and height of the image. This will help when we have different size images and we want watermark to appear at location relative to the edges of the image.
This code will add the text at left - bottom part of the image.
from PIL import Image, ImageDraw,ImageFont

path = "E:\\testing\\images\\certificate.png"  # blank certificate
path2 = "E:\\testing\\images\\certificate_1.png"# Path to save

img = Image.open(path, mode="r")
width,height=img.size # read height and width of main image 
#print(width,height)
margin_x,margin_y=40,125 # set margins from edges of the main image
fnt = ImageFont.truetype("times.ttf", 100) # family with size
d1 = ImageDraw.Draw(img)

d1.text((margin_x,height-margin_y), "plus2net", fill=(94, 61, 43),font=fnt)

img.show() # display 
img.save(path2) # save in given path 

Height and width of Font - String

Based on the font style and font size the total width and height of the string will change.
How to get the width and height of the string?
By using getmetrics() we will get a tuple of the font ascent (the distance from the baseline to the highest outline point) and descent (the distance from the baseline to the lowest outline point)
ascent, descent = fnt.getmetrics() 
getmask() Returns the bitmap for the text
getbbox() Returns bounding box (in pixels) of given text
text_width = fnt.getmask(text_string).getbbox()[2]  # height of the string
Here is the code to get width and height of a string with given font family and size.
from PIL import Image, ImageDraw,ImageFont

font_size=200 # Font size 
fnt = ImageFont.truetype("times.ttf", font_size) # family with size
ascent, descent = fnt.getmetrics() 

text_string='plus2net' # text to be writen 
text_width = fnt.getmask(text_string).getbbox()[2]  # height of the string
text_height = fnt.getmask(text_string).getbbox()[3] + descent # width 
print(text_width,text_height) #  width and height of the text writen
Using the above width and height based on the text string, we will create one image. This image will exactly hold our text string.
Watermark text with same Image Size
path = "F:\\testing\\images\\test3.png"  # new image to save 
img = Image.new("RGB", (text_width, text_height), color="#F1F1CC")
d1 = ImageDraw.Draw(img)
d1.text((0,0), text_string, fill=(194, 61, 43),font=fnt)
img.show()
#img.save(path)
Based on above concept we will add text string on all four corners of the image. Script will adjust the location of text string in all corners based on the font size, font family and length of the string.
Watermark in four corners of an Image
from PIL import Image, ImageDraw,ImageFont

path = "E:\\testing\\images\\certificate.png"  # blank certificate
path2 = "E:\\testing\\images\\certificate_1.png"# Path to save

text_string='plus2net' # text to be writen 
font_size=150 # font size 

fnt = ImageFont.truetype("times.ttf", font_size) # family with size
ascent, descent = fnt.getmetrics() 

text_width = fnt.getmask(text_string).getbbox()[2]  # height of the string
text_height = fnt.getmask(text_string).getbbox()[3] + descent # width 
print(text_width,text_height) # 

img = Image.open(path, mode="r")
width,height=img.size # read height and width of main image 
d1 = ImageDraw.Draw(img)
d1.text((width-text_width,height-text_height),text_string, fill=(94, 61, 43),font=fnt) # right buttom
d1.text((width-text_width,0),text_string, fill=(94, 61, 43),font=fnt) # right top 
d1.text((0,height-text_height),text_string, fill=(94, 61, 43),font=fnt) # left bottom
d1.text((0,0),text_string, fill=(94, 61, 43),font=fnt) # left top  
img.show() # display 
img.save(path2) # save in given path 

Adding rotated text as watermark

Watermark using text at an angle
from PIL import Image, ImageDraw, ImageFont, ImageOps

path = "F:\\testing\\images\\certificate.png"  # blank certificate
path2 = "F:\\testing\\images\\certificate_1.png"  # Path to save

img = Image.open(path, mode="r")
img2 = Image.new("L", (1200, 900))
fnt = ImageFont.truetype("times.ttf", 350)  # family with size
d1 = ImageDraw.Draw(img2)
d1.text((0, 0), "plus2net", fill=55, font=fnt)
w = img2.rotate(30, expand=1)
img.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 84)), (180, 120), w)
img.show()  # display
img.save(path2)  # save in given path

Adding image as watermark

Watermark using Image
from PIL import Image,ImageFilter

path = "F:\\testing\\images\\certificate.png"  # blank certificate
# path2 = "E:\\testing\\images\\cat-crop2.jpeg"  # Image to add
path2 = "F:\\testing\\images\\player.png"  # Image to add
path3 = "F:\\testing\\images\\certificate_1.png"  # final certificate
img = Image.open(path, mode="r")
img2 = Image.open(path2, mode="r")
# img2 = img2.filter(ImageFilter.EMBOSS) # add filter
img2 = img2.filter(ImageFilter.CONTOUR)
# img2 = img2.filter(ImageFilter.FIND_EDGES)
# img.paste(img2) # Past from top left corner
img.paste(img2, (200, 600))  # Past at x, y location
img.show()
img.save(path3)  # save final file
Adding Watermark using Images by Pillow library

Python Imaging Library PIL ImageDraw to add Line, rectangle, circle to an Image. ImageFilter
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer