Python Image Watermark – The Secret Ingredient to Visual Perfection

FREE Online Courses: Knowledge Awaits – Click for Free Access!

Watermarking an image is essential so that the original image remains distinct. Its purpose is to make unauthorized copying or use of original images difficult. So let’s make a project for the same.

What is Tkinter?

Python offers various utilities to design the GUI wiz Graphical User Interface, and one such utility is Tkinter which is most commonly used. It is one of the fastest and easiest ways to build GUI applications. Moreover, Tkinter is cross-platform. As a result, the same code works on macOS, Windows, and Linux.

Prerequisites for Image Watermark using Python

  • Basic knowledge of Python Programming Language how function is defined.
  • How the window is made in Tkinter GUI , how the frame is made in it.

Download Python Image Watermark Project

Please download the source code of Python Image Watermark Project from the following link: Python Image Watermark Project Code

Steps to Create Python Image Watermark Project

Step 1: Importing the necessary modules
Step 2: Making a window for our project
Step 3: Functions
Step 4: Function – def img1() & def img2()
Step 5: Function – def watermark()
Step 6: Making Frames and Mapping the Buttons to Their Functionalities

Step 1: Importing the necessary modules

To use Tkinter, we need to import the Tkinter module. We are also going to import the Image,ImageDraw, and ImageFont module, messagebox and filedialog module

Code:

#import packages
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageDraw, ImageFont

Step 2: Making a window for our project

This code sets the title of the window as ‘PythonGeeks Image Watermark’, and sets the dimensions ‘width x length’.

Code:

root = tk.Tk()
root.title("PythonGeeks Image Watermark")
root.geometry("500x500")

Step 3: Functions

This function takes input image to watermark, location where to save after its watermark is done, and text or logo to watermark on image.

Step 3.1: water1_1(input_img, output_img, text):

Code:

def water1_1(input_img, output_img, text):
   # Creates the Image object
   original_img = Image.open(input_img)
   width, height = original_img.size
   # Draw on Image
   watermark = ImageDraw.Draw(original_img)
   # Specify a font size
   font_size = int(width / 11)
   font = ImageFont.truetype("arial.ttf", font_size)
   x, y = int(width / 1.5), int(height / 1.1)
  # Add the watermark
   watermark.text((x, y), text, font=font,fill='#FFF', stroke_width=3, stroke_fill='#222', anchor='ms')
  # Saves the new image
   original_img.save(output_img)

Explanation:

Image.open and ImageDraw.Draw to open the image and watermark text on it with a watermark.text function, The text font, location (It is represented by x,y coordinates, which are calculated by varying the width and height of the original image.), width, and color by stroke_fill are assigned to the text in this function. ImageFont.truetype assigns its font and size and original_img.save saves the output image.

Step 3.1: watermark_image(input_img, output_img, text):

Code:

def water2_1(input_img, output_img, logo_img):
   original_img = Image.open(input_img)
   copied_image = original_img.copy()
   width, height = original_img.size
   x, y = int(width / 1.5), int(height / 1.1)
   ii = Image.open(logo_img)
   copied_image.paste(ii,(x,y))
   copied_image.save(output_img)

Explanation:

Image.open and original_img.copy to open the image and make a copy of the original. Again image.open to open the logo image and copied_image.paste to paste the logo on the original image in the location specified by x,y.

Step 4: Function – def img1() & def img2()

These functions take the path of an input image and an output image from the user with the use of the filedialog module.

Code:

def img1():
   # takes path of input image  
   path = filedialog.askopenfilename()
   entry1.delete(0, tk.END)
   entry1.insert(0, path)


def img2():
# takes path of output  image  
 path = filedialog.asksaveasfilename(defaultextension=".png")
   entry2.delete(0, tk.END)
   entry2.insert(0, path)

Step 5: Function – def watermark()

With the use of the previous functions that we made, we get three inputs from the user either for text or logo, and finally, the watermark function is executed and the image is watermarked.

Code:

def watermark():
   # assign values to the watermark_image function
   path1 = entry1.get()
   path2 = entry2.get()
   text = entry3.get()
   if not path1 or not path2 or not text and not path3):
       messagebox.showerror("Error", "All fields are required!")
       return
    if (text == True):
       water1_1(path1, path2, text)
   else:
       water2_1(path1, path2, path3)
   messagebox.showinfo("PythonGeeks(Congrats)", "Watermarked image saved!")

Step 6: Making Frames and Mapping the Buttons to Their Functionalities

We make four frames, and in each frame we give label, entry, and button modules to get input from the user input image, output image location, and watermark text or logo image.

Code:

#make three frames and for each frame three entry function
frame1 = tk.Frame(root)
frame1.pack(pady=20)


l1 = tk.Label(frame1, text="Image to Watermark:")
l1.pack(side="left", padx=10)
entry1 = tk.Entry(frame1, width=30)
entry1.pack(side="left")
button1 = tk.Button(frame1, text="Select",width=20, command=img1)
button1.pack(side="right", padx=10)


frame2 = tk.Frame(root)
frame2.pack(pady=20)


l2 = tk.Label(frame2, text="Save Watermarked Image")
l2.pack(side="left", padx=10)
entry2 = tk.Entry(frame2, width=30)
entry2.pack(side="left")
button2 = tk.Button(frame2, text="Select", width=20, command=img2)
button2.pack(side="right", padx=10)
frame3 = tk.Frame(root)
frame3.pack(pady=20)


l3 = tk.Label(frame3, text="Enter text")
l3.pack(side="left", padx=10)
entry3 = tk.Entry(frame3, width=30)
entry3.pack(side="left")
button3 = tk.Button(root, width=20, text="Watermark", command=watermark)
button3.pack(expand=True)
root.mainloop()


frame4 = tk.Frame(root)
frame4.pack(pady=20)
l4 = tk.Label(frame4, text="logo to be watermarked:")
l4.pack(side="left", padx=10)
entry4 = tk.Entry(frame4, width=30)
entry4.pack(side="left")
button4 = tk.Button(frame4, text="Select", width=20, command=img3)
button4.pack(side="right", padx=10)

Full Code:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageDraw, ImageFont




def water1_1(input_img, output_img, text):
   original_img = Image.open(input_img)
   width, height = original_img.size
   watermark = ImageDraw.Draw(original_img)


   font_size = int(width / 11)
   font = ImageFont.truetype("arial.ttf", font_size)
   x, y = int(width / 1.5), int(height / 1.1)
   watermark.text((x, y), text, font=font, fill='#FFF', stroke_width=3, stroke_fill='#222', anchor='ms')
   original_img.save(output_img)




def water2_1(input_img, output_img, logo_img):
   original_img = Image.open(input_img)
   copied_image = original_img.copy()
   width, height = original_img.size
   x, y = int(width / 1.5), int(height / 1.1)
   ii = Image.open(logo_img)
   copied_image.paste(ii,(x,y))
   copied_image.save(output_img)




def img1():
   path = filedialog.askopenfilename()
   entry1.delete(0, tk.END)
   entry1.insert(0, path)




def img2():
   path = filedialog.asksaveasfilename(defaultextension=".png")
   entry2.delete(0, tk.END)
   entry2.insert(0, path)




def img3():
   path = filedialog.askopenfilename()
   entry4.delete(0, tk.END)
   entry4.insert(0, path)




def watermark():
   path1 = entry1.get()
   path2 = entry2.get()
   path3 = entry4.get()
   text = entry3.get()
   if not path1 or not path2 or (not text and not path3):
       messagebox.showerror("Error", "All fields are required!")
       return
   if (text == True):
       water1_1(path1, path2, text)
   else:
       water2_1(path1, path2, path3)
   messagebox.showinfo("PythonGeeks(Congrats)", "Watermarked image saved!")




root = tk.Tk()
root.title("PythonGeeks Image Watermark")
root.geometry("500x500")


frame1 = tk.Frame(root)
frame1.pack(pady=20)


l1 = tk.Label(frame1, text="Image to Watermark:")
l1.pack(side="left", padx=10)
entry1 = tk.Entry(frame1, width=30)
entry1.pack(side="left")
button1 = tk.Button(frame1, text="Select", width=20, command=img1)
button1.pack(side="right", padx=10)




frame4 = tk.Frame(root)
frame4.pack(pady=20)
l4 = tk.Label(frame4, text="logo to be watermarked:")
l4.pack(side="left", padx=10)
entry4 = tk.Entry(frame4, width=30)
entry4.pack(side="left")
button4 = tk.Button(frame4, text="Select", width=20, command=img3)
button4.pack(side="right", padx=10)




frame2 = tk.Frame(root)
frame2.pack(pady=20)


l2 = tk.Label(frame2, text="Save Watermarked Image")
l2.pack(side="left", padx=10)
entry2 = tk.Entry(frame2, width=30)
entry2.pack(side="left")
button2 = tk.Button(frame2, text="Select", width=20, command=img2)
button2.pack(side="right", padx=10)




frame3 = tk.Frame(root)
frame3.pack(pady=20)


l3 = tk.Label(frame3, text="Enter text")
l3.pack(side="left", padx=10)
entry3 = tk.Entry(frame3, width=30)
entry3.pack(side="left")
button3 = tk.Button(root, width=20, text="Watermark", command=watermark)
button3.pack(expand=True)


root.mainloop()

Final screens of the Image Watermarker:

image watermark

image watermark project

Before and After Images-

image watermark output

image watermark project output

Summary:

We have successfully created an Image Watermark using Python Tkinter GUI. This project provides practical exposure to various Python libraries.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook


Leave a Reply

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