How can we create a user-friendly desktop application that enables users to upload images of their choice and easily add a watermark to these images? The program should provide a graphical user interface (GUI) for selecting image files, applying a watermark with user-defined text, and saving the watermarked images to the user’s preferred location. Additionally, how can we ensure that the application supports popular image formats and provides a seamless experience for both novice and experienced users?
Solution code
# import necessary libraries to process the image
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageDraw, ImageFont
from PIL import Image, ImageDraw, ImageFont
def add_watermark(input_image_path, output_image_path, watermark_text):
image = Image.open(input_image_path)
draw = ImageDraw.Draw(image)
width, height = image.size
font = ImageFont.truetype('arial.ttf', 36)
text_width, text_height = 10,10 # Use textsize from ImageDraw
margin = 10
x = width - text_width - margin
y = height - text_height - margin
draw.text((x, y), watermark_text, fill='white', font=font)
image.save(output_image_path)
# OUTPUT THE IMAGE
# function to open a file dialog and process image
def open_file_dialog():
file_path = filedialog.askopenfilename()
if file_path:
watermark_text = "Your watermark"
output_path = "output_image.jpg" # Specify the correct output path
add_watermark(file_path, output_path, watermark_text)
result_label.config(text="Watermarked image saved as 'output_image.jpg'")
# create the main window
root = tk.Tk()
root.title("Image WaterMark App")
# create and configure the upload button
upload_button = tk.Button(root, text="Upload Image", command=open_file_dialog)
upload_button.pack()
# create a label to display the result
result_label = tk.Label(root, text="")
result_label.pack()
# start the GUI application
root.mainloop()