Synced repo using 'sync_with_huggingface' Github Action
Browse files
app.py
CHANGED
@@ -1,7 +1,101 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
from image_processor import process_image
|
6 |
|
|
|
|
|
7 |
|
8 |
+
def apply_standard_settings(setting):
|
9 |
+
"""Returns the parameters for the selected standard setting."""
|
10 |
+
settings_dict = {
|
11 |
+
"S light": (True, True, "240x240", 48, "whitesmoke"),
|
12 |
+
"M light": (True, True, "480x480", 96, "whitesmoke"),
|
13 |
+
"L light": (True, True, "960x960", 128, "whitesmoke"),
|
14 |
+
"S dark": (True, True, "240x240", 48, "#2A373D"),
|
15 |
+
"M dark": (True, True, "480x480", 96, "#2A373D"),
|
16 |
+
"L dark": (True, True, "960x960", 128, "#2A373D"),
|
17 |
+
}
|
18 |
+
# Default to no special settings
|
19 |
+
return settings_dict.get(setting, (None, None, None, None, None))
|
20 |
+
|
21 |
+
|
22 |
+
def settings_description(crop, remove_bg, resize, padding, background):
|
23 |
+
"""Generate an HTML text description of the current settings in a smaller font and list format."""
|
24 |
+
description = f"""
|
25 |
+
<ul style="font-size:small;">
|
26 |
+
<li>Crop: {crop}</li>
|
27 |
+
<li>Remove Background: {remove_bg}</li>
|
28 |
+
<li>Resize: {resize if resize else 'No resize'}</li>
|
29 |
+
<li>Padding: {padding}</li>
|
30 |
+
<li>Background: {background}</li>
|
31 |
+
</ul>
|
32 |
+
"""
|
33 |
+
return description
|
34 |
+
|
35 |
+
|
36 |
+
def gradio_interface(image, standard_settings, crop=False, remove_bg=False, resize=None, padding=0, background="white"):
|
37 |
+
# Apply standard settings if selected and not "None"
|
38 |
+
if image is None:
|
39 |
+
# Load the standard image from the specified path if no image is uploaded
|
40 |
+
standard_image_path = 'https://f.uguu.se/FMSiaqYp.png'
|
41 |
+
image = Image.open(standard_image_path)
|
42 |
+
|
43 |
+
if standard_settings and standard_settings != "None":
|
44 |
+
crop, remove_bg, resize, padding, background = apply_standard_settings(
|
45 |
+
standard_settings)
|
46 |
+
|
47 |
+
# Generate settings description
|
48 |
+
applied_settings = settings_description(
|
49 |
+
crop, remove_bg, resize, padding, background)
|
50 |
+
|
51 |
+
# Convert resize string to tuple (if provided)
|
52 |
+
resize_dimensions = None
|
53 |
+
if resize:
|
54 |
+
try:
|
55 |
+
width, height = map(int, resize.split('x'))
|
56 |
+
resize_dimensions = (width, height)
|
57 |
+
except ValueError:
|
58 |
+
return "Invalid format for resize dimensions. Please use 'WxH'.", "original", applied_settings
|
59 |
+
# Process the image directly
|
60 |
+
processed_image = process_image(
|
61 |
+
image, crop, remove_bg, resize_dimensions, padding, background)
|
62 |
+
|
63 |
+
# Generate settings description
|
64 |
+
applied_settings = settings_description(
|
65 |
+
crop, remove_bg, resize, padding, background)
|
66 |
+
|
67 |
+
return processed_image, applied_settings
|
68 |
+
|
69 |
+
|
70 |
+
example_images = [
|
71 |
+
["https://f.uguu.se/FMSiaqYp.png", "S light", True, True, "480x420", 10, "whitesmoke"],
|
72 |
+
["https://f.uguu.se/RKKYgeDC.png", "None", True, True, "480x320", 48, "blue"],
|
73 |
+
["https://f.uguu.se/WMoKEkYc.png", "None", True, True, "360x360", 48, "yellow"],
|
74 |
+
]
|
75 |
+
|
76 |
+
# Define the Gradio interface
|
77 |
+
interface = gr.Interface(fn=gradio_interface,
|
78 |
+
inputs=[
|
79 |
+
gr.components.Image(
|
80 |
+
type="pil", label="Input Image"),
|
81 |
+
gr.components.Radio(choices=[
|
82 |
+
"None", "S light", "M light", "L light", "S dark", "M dark", "L dark"], label="Settings"),
|
83 |
+
gr.components.Checkbox(label="Crop"),
|
84 |
+
gr.components.Checkbox(label="Remove Background"),
|
85 |
+
gr.components.Textbox(
|
86 |
+
label="Resize (WxH)", placeholder="Example: 100x100"),
|
87 |
+
gr.components.Slider(
|
88 |
+
minimum=0, maximum=200, label="Padding"),
|
89 |
+
gr.components.Textbox(
|
90 |
+
label="Background", placeholder="Color name or hex code")
|
91 |
+
],
|
92 |
+
outputs=[
|
93 |
+
gr.components.Image(type="pil"),
|
94 |
+
gr.components.HTML(label="Applied Settings")
|
95 |
+
],
|
96 |
+
examples=example_images,
|
97 |
+
title="IMAGER ___ Image Processor",
|
98 |
+
description="Upload an image and select processing options or choose a standard setting. Supports crop, autoremove background, resize, add padding, and set the background color.",)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
interface.launch()
|