One of the easiest ways to create a watermarked image using Python is by using the PIL (Python Imaging Library) library. Here's a step-by-step guide on how to do it:
-
Install the PIL library using pip by running the following command in your terminal:
pip install pillow
-
Import the required modules and open the original image:
from PIL import Image, ImageDraw, ImageFont
original_image = Image.open('path_to_original_image.jpg')
-
Create a copy of the original image to apply the watermark on:
watermarked_image = original_image.copy()
-
Create a ImageDraw object to draw the watermark text onto the image:
draw = ImageDraw.Draw(watermarked_image)
-
Define the watermark text and font properties:
watermark_text = "Your Watermark Text"
font = ImageFont.truetype('path_to_font.ttf', size=40)
Note: Replace 'path_to_font.ttf' with the path to the font file you want to use for the watermark.
-
Calculate the position to place the watermark text using the textsize method:
text_width, text_height = draw.textsize(watermark_text, font=font)
x = (watermarked_image.width - text_width) // 2
y = (watermarked_image.height - text_height) // 2
-
Draw the watermark text onto the image:
draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255, 128))
Note: You can customize the color and transparency of the text by changing the fill parameter. The (255, 255, 255, 128) values represent white color with 50% transparency.
-
Save the watermarked image:
watermarked_image.save('path_to_save_watermarked_image.jpg')
Note: Replace 'path_to_save_watermarked_image.jpg' with the desired file path and name for the watermarked image.
That's it! You have now created a watermarked image using Python.