Python Pillow – Creating a Watermark


Python Pillow – Creating a Watermark



”;


What is Watermark?

A watermark is a recognizable and often transparent image or text that is superimposed onto another image, document or object to indicate ownership, authorship or origin. Watermarks are typically used to protect intellectual property and content which prevent unauthorized use or distribution and provide attribution. They serve various purposes as mentioned below −

  • Copyright Protection − Artists, photographers and content creators often use watermarks to protect their intellectual property by marking their work with their name, logo or copyright information. This helps deter unauthorized use or distribution of their content.

  • Branding − Companies and organizations use watermarks to brand their images or documents with their logos, names or slogans. This reinforces their brand identity and makes it clear where the content originated.

  • Document Verification − Watermarks can be used on official documents such as certificates to prevent forgery or unauthorized reproduction. For example diplomas or notarized documents may have watermarks.

  • Security − In currency and other security documents intricate and often invisible watermarks are used to deter counterfeiting. These watermarks are difficult to reproduce accurately making it easier to detect counterfeit bills or documents.

  • Image Attribution − In the context of stock photography and image licensing watermarks can be used to display a preview of the image with a watermark. When a user purchases the image they receive a version without the watermark.

  • Digital Media − In the digital world watermarks are often used on images and videos shared online to protect content. They can also be used to give credit to the original creator.

Watermarks can take various forms such as text, logos, patterns or even invisible data embedded within the content. They are typically placed in a manner that is difficult to remove without compromising the quality of the content and their purpose is to provide a visual or digital indicator of authenticity or ownership.

Creating the text watermark

Now let”s explore how to create a text watermark by using the pillow library. In pillow there is no direct method to create the watermarks but we can achieve it by using the ImageDraw, ImageFont and Image methods.

Following is the input image used in all the examples of this chapter.


butterfly

Example

In this example we are creating the text Tutorialspoint as the watermark by using the pillow library.


from PIL import Image, ImageDraw, ImageFont
original_image = Image.open("Images/butterfly.jpg")
draw = ImageDraw.Draw(original_image)
watermark_text = "Tutorialspoint"
font_size = 20
font = ImageFont.truetype("arial.ttf", font_size)  
text_color = (255, 255, 255)  

#White color (RGB)
text_width, text_height = draw.textsize(watermark_text, font)
image_width, image_height = original_image.size
margin = 10  

#Margin from the right and bottom edges
position = (image_width - text_width - margin, image_height - text_height - margin)
draw.text(position, watermark_text, font=font, fill=text_color)
original_image.save("output Image/watermarked_image.png")
original_image.show() 

Output


watermarked

Creating the image Watermark

Previously we created the text watermark on an image, in the same way we can create an image as the watermark by using the ImageDraw, copy and paste methods available in pillow.

Example

In this example we are creating the Tutoriaslpoint logo image as the watermark by the methods available in pillow.


from PIL import Image
original_image = Image.open("Images/butterfly.jpg")
watermark = Image.open("Images/tutorialspoint.png")  
#Use the appropriate image file for your watermark

#Resize the watermark image to a specific width or height
target_width = 200  

#Adjust this to our preferred size
aspect_ratio = float(target_width) / watermark.width
target_height = int(watermark.height * aspect_ratio)
watermark = watermark.resize((target_width, target_height), Image.ANTIALIAS)
watermarked_image = original_image.copy()

#Adjust the position where you want to place the watermark (e.g., bottom right corner)
position = (original_image.width - watermark.width, original_image.height - watermark.height)
watermarked_image.paste(watermark, position, watermark)
watermarked_image.save("output Image/watermarked_image.jpg")
watermarked_image.show()

Output


watermarked Tutorialspoint

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *