Skip to content

Commit 15e4b3a

Browse files
authored
PR #45: Image Watermarking Tool
adding water adder using python Merge pull request #45 from ramanuj-droid/water
2 parents 09ec755 + 2bb11e5 commit 15e4b3a

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

Image_watermark_Adder/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 🖋️ Image Watermarking Tool
2+
3+
A simple **Streamlit** app to add **text or logo watermarks** to images.
4+
5+
---
6+
7+
## 🚀 Features
8+
- Upload image (JPG/PNG)
9+
- Add **text** watermark with adjustable font size, position, and opacity
10+
- Add **logo** watermark with adjustable size, position, and transparency
11+
- Live preview before download
12+
- Download final image as JPG
13+
14+
---
15+
16+
## 🧱 Tech Stack
17+
- **Python 3**
18+
- **Streamlit**
19+
- **Pillow (PIL)**

Image_watermark_Adder/app.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import streamlit as st
2+
from PIL import Image
3+
import os
4+
from utils.watermark import add_text_watermark, add_logo_watermark
5+
6+
st.set_page_config(page_title=" Image Watermarking Tool", layout="wide")
7+
8+
st.title(" Image Watermarking Tool")
9+
st.write("Upload an image, add a text or logo watermark, and download the result.")
10+
11+
os.makedirs("output", exist_ok=True)
12+
13+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
14+
15+
if uploaded_file:
16+
image = Image.open(uploaded_file).convert("RGB")
17+
st.image(image, caption="Original Image", use_container_width=True)
18+
19+
st.sidebar.header(" Settings")
20+
wm_type = st.sidebar.radio("Watermark Type", ["Text", "Logo"])
21+
22+
position = st.sidebar.selectbox("Position", ["Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right", "Center"])
23+
opacity = st.sidebar.slider("Opacity", 0.0, 1.0, 0.5)
24+
25+
if wm_type == "Text":
26+
text = st.sidebar.text_input("Enter Watermark Text", "© MyBrand")
27+
font_size = st.sidebar.slider("Font Size", 20, 100, 40)
28+
29+
if st.sidebar.button("Apply Text Watermark"):
30+
result = add_text_watermark(image, text, position, opacity, font_size)
31+
output_path = os.path.join("output", "watermarked_text.jpg")
32+
result.save(output_path)
33+
st.image(result, caption="Watermarked Image", use_container_width=True)
34+
st.download_button("Download Image", data=open(output_path, "rb"), file_name="watermarked_text.jpg")
35+
36+
elif wm_type == "Logo":
37+
logo_file = st.sidebar.file_uploader("Upload Logo (PNG preferred)", type=["png"])
38+
scale = st.sidebar.slider("Logo Scale", 0.05, 0.5, 0.2)
39+
40+
if st.sidebar.button("Apply Logo Watermark"):
41+
if logo_file:
42+
logo_path = os.path.join("assets", "temp_logo.png")
43+
os.makedirs("assets", exist_ok=True)
44+
with open(logo_path, "wb") as f:
45+
f.write(logo_file.getbuffer())
46+
47+
result = add_logo_watermark(image, logo_path, position, opacity, scale)
48+
output_path = os.path.join("output", "watermarked_logo.jpg")
49+
result.save(output_path)
50+
st.image(result, caption="Watermarked Image", use_container_width=True)
51+
st.download_button("Download Image", data=open(output_path, "rb"), file_name="watermarked_logo.jpg")
52+
else:
53+
st.warning("Please upload a logo image.")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
streamlit
2+
pillow

Image_watermark_Adder/utils/__init__.py

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
2+
import os
3+
4+
def add_text_watermark(image, text, position, opacity=0.5, font_size=40):
5+
watermark = image.copy()
6+
drawable = ImageDraw.Draw(watermark)
7+
8+
try:
9+
font = ImageFont.truetype("arial.ttf", font_size)
10+
except:
11+
font = ImageFont.load_default()
12+
13+
bbox = drawable.textbbox((0, 0), text, font=font)
14+
textwidth = bbox[2] - bbox[0]
15+
textheight = bbox[3] - bbox[1]
16+
17+
width, height = image.size
18+
margin = 10
19+
positions = {
20+
"Top-Left": (margin, margin),
21+
"Top-Right": (width - textwidth - margin, margin),
22+
"Bottom-Left": (margin, height - textheight - margin),
23+
"Bottom-Right": (width - textwidth - margin, height - textheight - margin),
24+
"Center": ((width - textwidth) // 2, (height - textheight) // 2)
25+
}
26+
pos = positions.get(position, positions["Bottom-Right"])
27+
28+
# Draw text with opacity
29+
drawable.text(pos, text, fill=(255, 255, 255, int(255 * opacity)), font=font)
30+
return watermark
31+
32+
33+
34+
def add_logo_watermark(image, logo_path, position, opacity=0.5, scale=0.2):
35+
"""
36+
Add a logo watermark to the given image.
37+
"""
38+
base = image.convert("RGBA")
39+
logo = Image.open(logo_path).convert("RGBA")
40+
logo_width = int(base.width * scale)
41+
aspect_ratio = logo.height / logo.width
42+
logo_height = int(logo_width * aspect_ratio)
43+
logo = logo.resize((logo_width, logo_height))
44+
45+
46+
alpha = logo.split()[3]
47+
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
48+
logo.putalpha(alpha)
49+
50+
margin = 10
51+
positions = {
52+
"Top-Left": (margin, margin),
53+
"Top-Right": (base.width - logo_width - margin, margin),
54+
"Bottom-Left": (margin, base.height - logo_height - margin),
55+
"Bottom-Right": (base.width - logo_width - margin, base.height - logo_height - margin),
56+
"Center": ((base.width - logo_width) // 2, (base.height - logo_height) // 2)
57+
}
58+
pos = positions.get(position, positions["Bottom-Right"])
59+
60+
base.paste(logo, pos, logo)
61+
return base.convert("RGB")

0 commit comments

Comments
 (0)