Skip to content

Commit d03bb89

Browse files
authored
Merge pull request #145 from SSHSRN/imageWatermark
Watermark Generator
2 parents 1597b58 + c50174f commit d03bb89

11 files changed

+141
-0
lines changed

Image Watermark Generator/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
**WATERMARK GENERATOR**
2+
3+
**GOAL**
4+
This is a simple watermark generator created in python programming language. You can add your own watermark to any image.
5+
6+
7+
**DESCRIPTION**
8+
The goal is to add a watermark to any image irrespective of the image type and extension
9+
10+
To use this code download watermarkGenerator.py and resources in the same directory.
11+
+ Open and run the file watermarkGenerator.py
12+
+ enjoy the code :-D
13+
14+
To use the GUI version of this code, download watermarkGeneratorGUI.py and resources in the same directory.
15+
+ Open and run the file watermarkGeneratorGUI.py
16+
+ enjoy the code :-D
17+
18+
**WHAT I HAD DONE**
19+
In this code, I implemented the code by using the following concepts:
20+
+ Pillow module
21+
+ Tkinter module
22+
+ os module
23+
24+
**DEMONSTRATION**
25+
![image](./images/res.png)
26+
![image](./images/cmd.png)
27+
![image](./images/gui.png)
28+
![image](./images/selectedImage.png)
29+
![image](./images/generatedImage.png)
30+
![image](./lion_watermarked.jpg)
31+
32+
33+
I wish you enjoy guess this code :-D
34+
35+
**SRIHARI S**
63.6 KB
Loading
Loading
42.9 KB
Loading
2.75 MB
Loading
Loading

Image Watermark Generator/lion.jpg

201 KB
Loading
194 KB
Loading
196 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#Import required Image library
2+
from PIL import Image, ImageDraw, ImageFont
3+
4+
print("""
5+
Image Watermark Generator
6+
7+
This program will add a watermark to an image.
8+
To add a watermark to an image, save the image in the same folder as this program and enter the name of the image when prompted.
9+
The image with the generated watermark will be saved in the result folder.
10+
""")
11+
12+
#Get the image name from the user
13+
image_name = input("Enter the name of the image (Also include the extension): ")
14+
#Create an Image Object from an Image
15+
im = Image.open('Image Watermark Generator/'+image_name)
16+
width, height = im.size
17+
18+
draw = ImageDraw.Draw(im)
19+
text = input("Enter the text to be added as watermark: ")
20+
21+
fontsize = int(input("Enter the font size: (Default is 36 and max is 100) "))
22+
font = ImageFont.truetype('arial.ttf', fontsize)
23+
textwidth, textheight = draw.textsize(text, font)
24+
25+
# calculate the x,y coordinates of the text
26+
margin = 10
27+
x = width - textwidth - margin
28+
y = height - textheight - margin
29+
30+
# draw watermark in the bottom right corner
31+
draw.text((x, y), text, font=font)
32+
im.show()
33+
34+
#Save watermarked image
35+
im.save('Image Watermark Generator/result/watermark.jpg')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import tkinter as tk
2+
from tkinter import filedialog
3+
from tkinter import messagebox
4+
from PIL import Image, ImageDraw, ImageFont
5+
import os
6+
7+
class watermarkGeneratorGUI(tk.Frame):
8+
def __init__(self, master=None):
9+
super().__init__(master)
10+
self.master = master
11+
self.pack()
12+
self.create_widgets()
13+
14+
def create_widgets(self):
15+
self.label1 = tk.Label(self, text="Image Watermark Generator")
16+
self.label1.pack(side="top")
17+
self.label1.config(font=("Arial", 30))
18+
19+
self.label2 = tk.Label(self, text="Select an image to watermark")
20+
self.label2.pack(side="top")
21+
self.label2.config(font=("Arial", 15))
22+
self.label2.config(fg="blue")
23+
24+
self.button1 = tk.Button(self, text="Browse", command=self.browse_button)
25+
self.button1.pack(side="top")
26+
self.button1.config(font=("Arial", 15))
27+
self.button1.config(fg="brown")
28+
29+
self.label3 = tk.Label(self, text="Enter the text to watermark")
30+
self.label3.pack(side="top")
31+
self.label3.config(font=("Arial", 15))
32+
33+
self.entry1 = tk.Entry(self)
34+
self.entry1.pack(side="top")
35+
self.entry1.config(font=("Arial", 15))
36+
37+
self.button2 = tk.Button(self, text="Generate", command=self.generate_button)
38+
self.button2.pack(side="top")
39+
self.button2.config(font=("Arial", 15))
40+
41+
def browse_button(self):
42+
self.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
43+
self.label2.configure(text="Selected file: " + self.filename)
44+
45+
def generate_button(self):
46+
watermark_text = self.entry1.get()
47+
if watermark_text == "":
48+
messagebox.showerror("Error", "Please enter the text to watermark")
49+
else:
50+
self.label3.configure(text="Watermark text: " + watermark_text)
51+
self.label3.pack(side="top")
52+
self.generate_watermark(watermark_text)
53+
54+
def generate_watermark(self, watermark_text):
55+
image = Image.open(self.filename)
56+
draw = ImageDraw.Draw(image)
57+
image_width, image_height = image.size
58+
font = ImageFont.truetype("arial.ttf", 36)
59+
text_width, text_height = draw.textsize(watermark_text, font)
60+
margin = 10
61+
x = image_width - text_width - margin
62+
y = image_height - text_height - margin
63+
draw.text((x, y), watermark_text, font=font)
64+
image.save(os.path.splitext(self.filename)[0] + "_watermarked.jpg")
65+
self.label2.configure(text="Watermarked image saved as " + os.path.splitext(self.filename)[0] + "_watermarked.jpg")
66+
67+
root = tk.Tk()
68+
root.title("Image Watermark Generator")
69+
root.geometry("800x600")
70+
app = watermarkGeneratorGUI(master=root)
71+
app.mainloop()

0 commit comments

Comments
 (0)