Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,37 @@
|
|
| 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 |
-
# Handle API input format
|
| 7 |
-
if isinstance(main_image, dict) and 'data' in main_image:
|
| 8 |
-
# Extract base64 from API format
|
| 9 |
-
import base64
|
| 10 |
-
import io
|
| 11 |
-
base64_str = main_image['data']
|
| 12 |
-
if base64_str.startswith('data:'):
|
| 13 |
-
base64_str = base64_str.split(',')[1]
|
| 14 |
-
image_data = base64.b64decode(base64_str)
|
| 15 |
-
main_image = Image.open(io.BytesIO(image_data))
|
| 16 |
"""
|
| 17 |
Add logo to the top-right corner of the main image
|
| 18 |
|
| 19 |
Args:
|
| 20 |
-
main_image: PIL Image object
|
| 21 |
logo_size_percent: Size of logo as percentage of main image width (default 20%)
|
| 22 |
margin_percent: Margin from edges as percentage of main image width (default 2%)
|
| 23 |
"""
|
| 24 |
if main_image is None:
|
| 25 |
return None
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Load the logo
|
| 28 |
logo_path = "logo.png"
|
| 29 |
if not os.path.exists(logo_path):
|
|
@@ -58,7 +66,7 @@ def add_logo(main_image, logo_size_percent=20, margin_percent=2):
|
|
| 58 |
|
| 59 |
return result
|
| 60 |
|
| 61 |
-
# Create Gradio interface
|
| 62 |
iface = gr.Interface(
|
| 63 |
fn=add_logo,
|
| 64 |
inputs=[
|
|
@@ -67,7 +75,8 @@ iface = gr.Interface(
|
|
| 67 |
outputs=gr.Image(type="pil", label="Image with Logo"),
|
| 68 |
title="Logo Adder",
|
| 69 |
description="Upload an image to add logo.png to the top-right corner",
|
| 70 |
-
examples=None
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
def add_logo(main_image, logo_size_percent=20, margin_percent=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
Add logo to the top-right corner of the main image
|
| 10 |
|
| 11 |
Args:
|
| 12 |
+
main_image: PIL Image object, file path, or base64 string (for API)
|
| 13 |
logo_size_percent: Size of logo as percentage of main image width (default 20%)
|
| 14 |
margin_percent: Margin from edges as percentage of main image width (default 2%)
|
| 15 |
"""
|
| 16 |
if main_image is None:
|
| 17 |
return None
|
| 18 |
|
| 19 |
+
# Handle base64 input from API
|
| 20 |
+
if isinstance(main_image, str) and main_image.startswith('data:image'):
|
| 21 |
+
# Remove data URL prefix
|
| 22 |
+
base64_str = main_image.split(',')[1]
|
| 23 |
+
# Decode base64 to image
|
| 24 |
+
image_data = base64.b64decode(base64_str)
|
| 25 |
+
main_image = Image.open(io.BytesIO(image_data))
|
| 26 |
+
elif isinstance(main_image, str):
|
| 27 |
+
# Try to decode as plain base64 (without data URL prefix)
|
| 28 |
+
try:
|
| 29 |
+
image_data = base64.b64decode(main_image)
|
| 30 |
+
main_image = Image.open(io.BytesIO(image_data))
|
| 31 |
+
except:
|
| 32 |
+
# If it fails, assume it's a file path
|
| 33 |
+
main_image = Image.open(main_image)
|
| 34 |
+
|
| 35 |
# Load the logo
|
| 36 |
logo_path = "logo.png"
|
| 37 |
if not os.path.exists(logo_path):
|
|
|
|
| 66 |
|
| 67 |
return result
|
| 68 |
|
| 69 |
+
# Create Gradio interface with API mode
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=add_logo,
|
| 72 |
inputs=[
|
|
|
|
| 75 |
outputs=gr.Image(type="pil", label="Image with Logo"),
|
| 76 |
title="Logo Adder",
|
| 77 |
description="Upload an image to add logo.png to the top-right corner",
|
| 78 |
+
examples=None,
|
| 79 |
+
api_name="predict" # Explicit API name
|
| 80 |
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|