Sahar7888 commited on
Commit
6525763
·
verified ·
1 Parent(s): b2be8d9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # Function to add watermark (logo or text) to the image
7
+ def add_watermark(image, watermark_type, text=None, logo=None, opacity=1.0):
8
+ image_logow = image.resize((500, 300))
9
+ image_logow = np.array(image_logow.convert('RGB'))
10
+
11
+ h_image, w_image, _ = image_logow.shape
12
+
13
+ if watermark_type == "Logo" and logo is not None:
14
+ logo = logo.resize((int(w_image * 0.3), int(h_image * 0.1))) # Resize logo to 30% of the image width
15
+ logo = np.array(logo.convert('RGB'))
16
+ h_logo, w_logo, _ = logo.shape
17
+
18
+ # Center placement for logo
19
+ center_y = int(h_image / 2)
20
+ center_x = int(w_image / 2)
21
+ top_y = center_y - int(h_logo / 2)
22
+ left_x = center_x - int(w_logo / 2)
23
+ bottom_y = top_y + h_logo
24
+ right_x = left_x + w_logo
25
+
26
+ # Get ROI and add logo with opacity
27
+ roi = image_logow[top_y: bottom_y, left_x: right_x]
28
+ result = cv2.addWeighted(roi, 1, logo, opacity, 0)
29
+
30
+ # Replace the ROI on the image
31
+ image_logow[top_y: bottom_y, left_x: right_x] = result
32
+
33
+ return Image.fromarray(image_logow)
34
+
35
+ elif watermark_type == "Text" and text:
36
+ # Add text watermark at the bottom-right corner
37
+ cv2.putText(image_logow, text=text, org=(w_image - 100, h_image - 10), fontFace=cv2.FONT_HERSHEY_COMPLEX,
38
+ fontScale=0.6, color=(0, 0, 255), thickness=2, lineType=cv2.LINE_AA)
39
+
40
+ return Image.fromarray(image_logow)
41
+
42
+ return image # Return original image if no watermark added
43
+
44
+ # Create the Gradio interface
45
+ def gradio_interface():
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown("## Image Watermarking App")
48
+
49
+ gr.Markdown("""
50
+ ### Add Watermarks to Your Images
51
+ This application allows you to add custom watermarks to your images. You can either add a **Text Watermark** or use a **Logo Watermark**.
52
+
53
+ **Steps**:
54
+ 1. Upload your image.
55
+ 2. If using a logo watermark, upload your logo.
56
+ 3. Select the type of watermark (text or logo).
57
+ 4. Customize the watermark by adding text or adjusting logo opacity.
58
+ 5. See the output image with the watermark applied.
59
+ """)
60
+
61
+ # Upload original image and logo
62
+ image_input = gr.Image(label="Upload Image", type="pil") # Upload original image
63
+ logo_input = gr.Image(label="Upload Logo (optional, for Logo Watermark)", type="pil") # Upload logo (optional)
64
+
65
+ # Select watermark type and adjust parameters
66
+ watermark_type = gr.Radio(["Text", "Logo"], label="Watermark Type", value="Text")
67
+ text_input = gr.Textbox(label="Watermark Text (for Text Watermark)", value="MyArt.Inc", placeholder="Enter text for text watermark")
68
+ opacity_slider = gr.Slider(0.1, 1.0, step=0.1, label="Logo Opacity (for Logo Watermark)", value=1.0)
69
+
70
+ # Output watermarked image
71
+ output_image = gr.Image(label="Watermarked Image")
72
+
73
+ # Function to update the watermark based on user input
74
+ def update_watermark(image, watermark_type, text, logo, opacity):
75
+ if watermark_type == "Logo" and logo is None:
76
+ return "Please upload a logo for the logo watermark."
77
+ return add_watermark(image, watermark_type, text, logo, opacity)
78
+
79
+ # Link inputs to output
80
+ inputs = [image_input, watermark_type, text_input, logo_input, opacity_slider]
81
+ gr.Button("Apply Watermark").click(update_watermark, inputs, output_image)
82
+
83
+ demo.launch()
84
+
85
+ # Run the interface
86
+ gradio_interface()