diff --git a/Image_watermark_Adder/README.md b/Image_watermark_Adder/README.md new file mode 100644 index 0000000..e420fe4 --- /dev/null +++ b/Image_watermark_Adder/README.md @@ -0,0 +1,19 @@ +# 🖋️ Image Watermarking Tool + +A simple **Streamlit** app to add **text or logo watermarks** to images. + +--- + +## 🚀 Features +- Upload image (JPG/PNG) +- Add **text** watermark with adjustable font size, position, and opacity +- Add **logo** watermark with adjustable size, position, and transparency +- Live preview before download +- Download final image as JPG + +--- + +## 🧱 Tech Stack +- **Python 3** +- **Streamlit** +- **Pillow (PIL)** \ No newline at end of file diff --git a/Image_watermark_Adder/app.py b/Image_watermark_Adder/app.py new file mode 100644 index 0000000..0a9bccc --- /dev/null +++ b/Image_watermark_Adder/app.py @@ -0,0 +1,53 @@ +import streamlit as st +from PIL import Image +import os +from utils.watermark import add_text_watermark, add_logo_watermark + +st.set_page_config(page_title=" Image Watermarking Tool", layout="wide") + +st.title(" Image Watermarking Tool") +st.write("Upload an image, add a text or logo watermark, and download the result.") + +os.makedirs("output", exist_ok=True) + +uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) + +if uploaded_file: + image = Image.open(uploaded_file).convert("RGB") + st.image(image, caption="Original Image", use_container_width=True) + + st.sidebar.header(" Settings") + wm_type = st.sidebar.radio("Watermark Type", ["Text", "Logo"]) + + position = st.sidebar.selectbox("Position", ["Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right", "Center"]) + opacity = st.sidebar.slider("Opacity", 0.0, 1.0, 0.5) + + if wm_type == "Text": + text = st.sidebar.text_input("Enter Watermark Text", "© MyBrand") + font_size = st.sidebar.slider("Font Size", 20, 100, 40) + + if st.sidebar.button("Apply Text Watermark"): + result = add_text_watermark(image, text, position, opacity, font_size) + output_path = os.path.join("output", "watermarked_text.jpg") + result.save(output_path) + st.image(result, caption="Watermarked Image", use_container_width=True) + st.download_button("Download Image", data=open(output_path, "rb"), file_name="watermarked_text.jpg") + + elif wm_type == "Logo": + logo_file = st.sidebar.file_uploader("Upload Logo (PNG preferred)", type=["png"]) + scale = st.sidebar.slider("Logo Scale", 0.05, 0.5, 0.2) + + if st.sidebar.button("Apply Logo Watermark"): + if logo_file: + logo_path = os.path.join("assets", "temp_logo.png") + os.makedirs("assets", exist_ok=True) + with open(logo_path, "wb") as f: + f.write(logo_file.getbuffer()) + + result = add_logo_watermark(image, logo_path, position, opacity, scale) + output_path = os.path.join("output", "watermarked_logo.jpg") + result.save(output_path) + st.image(result, caption="Watermarked Image", use_container_width=True) + st.download_button("Download Image", data=open(output_path, "rb"), file_name="watermarked_logo.jpg") + else: + st.warning("Please upload a logo image.") diff --git a/Image_watermark_Adder/requirements.txt b/Image_watermark_Adder/requirements.txt new file mode 100644 index 0000000..ff7db04 --- /dev/null +++ b/Image_watermark_Adder/requirements.txt @@ -0,0 +1,2 @@ +streamlit +pillow diff --git a/Image_watermark_Adder/utils/__init__.py b/Image_watermark_Adder/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Image_watermark_Adder/utils/watermark.py b/Image_watermark_Adder/utils/watermark.py new file mode 100644 index 0000000..2476a70 --- /dev/null +++ b/Image_watermark_Adder/utils/watermark.py @@ -0,0 +1,61 @@ +from PIL import Image, ImageDraw, ImageFont, ImageEnhance +import os + +def add_text_watermark(image, text, position, opacity=0.5, font_size=40): + watermark = image.copy() + drawable = ImageDraw.Draw(watermark) + + try: + font = ImageFont.truetype("arial.ttf", font_size) + except: + font = ImageFont.load_default() + + bbox = drawable.textbbox((0, 0), text, font=font) + textwidth = bbox[2] - bbox[0] + textheight = bbox[3] - bbox[1] + + width, height = image.size + margin = 10 + positions = { + "Top-Left": (margin, margin), + "Top-Right": (width - textwidth - margin, margin), + "Bottom-Left": (margin, height - textheight - margin), + "Bottom-Right": (width - textwidth - margin, height - textheight - margin), + "Center": ((width - textwidth) // 2, (height - textheight) // 2) + } + pos = positions.get(position, positions["Bottom-Right"]) + + # Draw text with opacity + drawable.text(pos, text, fill=(255, 255, 255, int(255 * opacity)), font=font) + return watermark + + + +def add_logo_watermark(image, logo_path, position, opacity=0.5, scale=0.2): + """ + Add a logo watermark to the given image. + """ + base = image.convert("RGBA") + logo = Image.open(logo_path).convert("RGBA") + logo_width = int(base.width * scale) + aspect_ratio = logo.height / logo.width + logo_height = int(logo_width * aspect_ratio) + logo = logo.resize((logo_width, logo_height)) + + + alpha = logo.split()[3] + alpha = ImageEnhance.Brightness(alpha).enhance(opacity) + logo.putalpha(alpha) + + margin = 10 + positions = { + "Top-Left": (margin, margin), + "Top-Right": (base.width - logo_width - margin, margin), + "Bottom-Left": (margin, base.height - logo_height - margin), + "Bottom-Right": (base.width - logo_width - margin, base.height - logo_height - margin), + "Center": ((base.width - logo_width) // 2, (base.height - logo_height) // 2) + } + pos = positions.get(position, positions["Bottom-Right"]) + + base.paste(logo, pos, logo) + return base.convert("RGB")