Python to take an Image and add Watermark with Full Source Code

Description:

This Project will take an image and add the desired watermark to it.

About this Project:

This project uses the PIL module and OS module to add the watermark. PIL, the Python Imaging Library, is an open-source library that enables the users to add, manipulate and save different file formats.

Requirements:

  • PIL==1.1.6

Usage:

Use the Script watermark.py. In the command line, Enter

python3 watermark.py [image_path]
Code language: CSS (css)

Replace the [image_path] with the image you want to add watermark to.

The output will be the image with the desired watermark.

Source Code:

watermark.py

import os from PIL import Image from PIL import ImageFilter def watermark_photo(input_image_path, output_image_path, watermark_image_path): base_image = Image.open(input_image_path) watermark = Image.open(watermark_image_path) # add watermark to your image position = base_image.size watermark.size newsize = int(position[0] * 8 / 100), int(position[0] * 8 / 100) watermark = watermark.resize(newsize) # Blur If Needed # watermark = watermark.filter(ImageFilter.BoxBlur(2)) new_position = position[0] - newsize[0] - 20, position[1] - newsize[1] - 20 transparent = Image.new(mode="RGBA", size=position, color=(0, 0, 0, 0)) # Create a new transparent image transparent.paste(base_image, (0, 0)) # paste the original image transparent.paste(watermark, new_position, mask=watermark) # paste the watermark image image_mode = base_image.mode if image_mode == "RGB": transparent = transparent.convert(image_mode) else: transparent = transparent.convert("P") transparent.save(output_image_path, optimize=True, quality=100) print("Saving " + output_image_path + " ...") folder = input("Enter Folder Path : ") watermark = input("Enter Watermark Path : ") os.chdir(folder) files = os.listdir(os.getcwd()) if not os.path.isdir("output"): os.mkdir("output") c = 1 for f in files: if os.path.isfile(os.path.abspath(f)): if f.endswith(".png") or f.endswith(".jpg"): watermark_photo(f, "output/" + f, watermark)
Code language: PHP (php)

Leave a Comment