EdysorEdutech commited on
Commit
dae6eac
·
verified ·
1 Parent(s): 8afb1b6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import os
4
+
5
+ def add_logo(main_image, logo_size_percent=20, margin_percent=2):
6
+ """
7
+ Add logo to the top-right corner of the main image
8
+
9
+ Args:
10
+ main_image: PIL Image object or file path
11
+ logo_size_percent: Size of logo as percentage of main image width (default 20%)
12
+ margin_percent: Margin from edges as percentage of main image width (default 2%)
13
+ """
14
+ if main_image is None:
15
+ return None
16
+
17
+ # Load the logo
18
+ logo_path = "logo.png"
19
+ if not os.path.exists(logo_path):
20
+ return main_image # Return original if logo not found
21
+
22
+ # Open images
23
+ logo = Image.open(logo_path).convert("RGBA")
24
+
25
+ # Convert main image to RGBA to support transparency
26
+ main_img = main_image.convert("RGBA")
27
+
28
+ # Calculate logo size (maintain aspect ratio)
29
+ logo_width = int(main_img.width * logo_size_percent / 100)
30
+ logo_height = int(logo.height * (logo_width / logo.width))
31
+
32
+ # Resize logo
33
+ logo = logo.resize((logo_width, logo_height), Image.Resampling.LANCZOS)
34
+
35
+ # Calculate position (top-right corner with margin)
36
+ margin = int(main_img.width * margin_percent / 100)
37
+ x_position = main_img.width - logo_width - margin
38
+ y_position = margin
39
+
40
+ # Create a copy of the main image
41
+ result = main_img.copy()
42
+
43
+ # Paste logo with transparency
44
+ result.paste(logo, (x_position, y_position), logo)
45
+
46
+ # Convert back to RGB for saving/display
47
+ result = result.convert("RGB")
48
+
49
+ return result
50
+
51
+ # Create Gradio interface
52
+ iface = gr.Interface(
53
+ fn=add_logo,
54
+ inputs=[
55
+ gr.Image(type="pil", label="Upload Main Image")
56
+ ],
57
+ outputs=gr.Image(type="pil", label="Image with Logo"),
58
+ title="Logo Adder",
59
+ description="Upload an image to add logo.png to the top-right corner",
60
+ examples=None
61
+ )
62
+
63
+ if __name__ == "__main__":
64
+ iface.launch()