You are on page 1of 6

Image Watermarking Tool User Manual

With Complete Code

Kalyan Chatterjea
April 24, 2024

1 Introduction
This user manual guides you through using a Python program that adds water-
marks to images. A watermark is a semi-transparent image or text overlaid on
another image, often used to indicate ownership or branding.

2 Benefits
• Protection: Watermarks help protect images from unauthorized use by
adding a visible mark of ownership.

• Branding: You can use watermarks to promote your brand or website


by adding a logo or signature.
• Customization: The program allows you to choose the watermark image,
position, and size for a personalized touch.

3 System Requirements
• Python 3 (download from https://www.python.org/downloads/)
• Python Imaging Library (PIL) or Pillow library (install using pip install
Pillow)

4 Usage
4.1 Prepare Watermark Image
Prepare a watermark image in JPEG or PNG format. This could be your logo
or signature.

1
4.2 Run the Program
1. Open a terminal or command prompt.
2. Navigate to the directory containing the program.

3. Run the program using: python watermarking program.py

4.3 Select Image Folder


1. The program will prompt you to select a folder containing the images you
want to watermark.
2. Use the file selection dialog to choose the folder.

4.4 Choose Watermark Position


1. Enter the desired position for the watermark:
• 1: Top left
• 2: Bottom left
• 3: Top right
• 4: Bottom right

4.5 View Watermarked Images


• Watermarked images will be saved in the selected folder with a prefix
”wm-” added to their original filenames.
• The program will print success messages for each watermarked image and
any error messages encountered.

5 Function of K
The variable K in the program represents the padding around the watermark.
It determines the spacing between the watermark and the edges of the image.
By adjusting the value of K, you can control the size of the watermark and its
distance from the edges of the image.

6 Conclusion
The watermarking program offers a simple yet effective way to protect your
images and promote your brand. Follow the instructions in this manual to use
the program successfully.
For more information, visit: https://www.tutorialspoint.com/python_
pillow/python_pillow_creating_a_watermark.htm

2
7 Photo Example with Watermark

Figure 1: Example Image with Watermark

3
8 Program Code in 3 Listings

Listing 1: Python code with comments


import os # Import the os module for file
operations
from PIL import Image # Import the Image module
from the PIL library

K = 20 # Define K with a value of 20

def add_watermark(image_path, signature_path,


position):
"""
This function adds a watermark to an image at a
specified position.

Args:
image_path (str): Path to the image file.
signature_path (str): Path to the watermark image
file.
position (int): Position of the watermark (1: top
left, 2: bottom left, 3: top right, 4: bottom
right).

Raises:
ValueError: If an invalid position is provided.
Exception: Any other error that occurs during
processing.
"""
try:
# Open the image and signature files
image = Image.open(image_path) # Open the image
file
signature = Image.open(signature_path).convert("
RGBA") # Open the signature file and convert to
RGBA mode

# Resize the signature image if necessary to fit on


the image
width, height = image.size # Get the dimensions of
the image
signature_width, signature_height = signature.size
# Get the dimensions of the signature
resize_factor = min(width / 10 / signature_width,
height / 10 / signature_height) # Calculate the
resize factor

4
Listing 2: Python code with comments - contd.

# Define the position to overlay the signature on the image


if position == 1: # Top left corner
offset = (K, K)
elif position == 2: # Bottom left corner
offset = (K, height - resized_signature.height - K)
elif position == 3: # Top right corner
offset = (width - resized_signature.width - K, K)
elif position == 4: # Bottom right corner
offset = (width - resized_signature.width - K, height -
resized_signature.height - K)
else:
raise ValueError("Invalid position. Please choose a value
between 1 and 4.")

# Overlay the signature onto the image


image.paste(resized_signature, offset, resized_signature)

# Save the watermarked image with a new filename


output_filename = f"wm-{os.path.basename(image_path)}" #
Construct the output filename
output_path = os.path.join(os.path.dirname(image_path),
output_filename) # Construct the output path
image.save(output_path) # Preserve original format
print(f"Watermarked image saved: {output_path}") # Print a
success message
except Exception as e:
print(f"Error: {e}") # Print any errors that occur

def select_folder(default_folder="/home/kalyansg/Pictures")
:
"""
This function prompts the user to select a folder.

Args:
default_folder (str): Default folder path (default is "/
home/kalyansg/Pictures").

Returns:
str: Path to the selected folder.
"""
from tkinter import filedialog, Tk # Import necessary
modules
root = Tk() # Create Tkinter root window
root.withdraw() # Hide the main window

5
Listing 3: Python code with comments - contd.

folder_path = filedialog.askdirectory(title="Select Folder"


, initialdir=default_folder) # Open folder selection
dialog with default folder
root.destroy() # Destroy the root window after selection
return folder_path # Return the selected folder path

def main():
"""
This function is the main entry point of the program. It
prompts the user for input and processes images in the
selected folder.
"""
folder_path = select_folder() # Select a folder for
processing
if not folder_path:
print("No folder selected. Exiting program.") # Print
message if no folder selected and exit
return

# Get the directory path of the current Python script


script_dir = os.path.dirname(os.path.realpath(__file__))
# Construct the path to the signature image
signature_path = os.path.join(script_dir, "SignatureKC.jpg"
)
# Prompt user to enter position for watermark
position = int(input("Enter the position of the watermark
(1-top left, 2-bottom left, 3-top right, 4-bottom
right): "))
for filename in os.listdir(folder_path):from tkinter
import filedialog, Tk # Import necessary modules
root = Tk() # Create Tkinter root window
root.withdraw() # Hide the main window
folder_path = filedialog.askdirectory(title="Select Folder"
, initialdir=default_folder) # Open folder selection
dialog with default folder
root.destroy() # Destroy the root window after selection
return folder_path # Return the selected folder path
if filename.lower().endswith((’.jpg’, ’.jpeg’, ’.png’)): #
Check if file is an image
image_path = os.path.join(folder_path, filename) #
Construct full path to the image
add_watermark(image_path, signature_path, position) # Add
watermark to the image
if __name__ == "__main__":
main() # Call the main function if script is executed
directly

You might also like