Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
comdoleger
commited on
Commit
•
647a13f
1
Parent(s):
c545b69
Update app.py
Browse files
app.py
CHANGED
@@ -68,19 +68,45 @@ def process(data, api, api_key):
|
|
68 |
|
69 |
return image
|
70 |
|
71 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
size = fore.size
|
|
|
|
|
|
|
74 |
if size[0]*size[1]<=(768*768):
|
75 |
gr.Warning("ℹ️ The input image resolution is low, it might lead to some deformation!")
|
76 |
|
77 |
if size[0]*size[1]>(1500*1500):
|
78 |
gr.Warning("ℹ️ The input image size is too big, I will lower it!")
|
79 |
-
|
80 |
-
|
81 |
|
82 |
|
83 |
-
|
84 |
data = {
|
85 |
"foreground_image64": forestr,
|
86 |
"prompt" : prompt,
|
@@ -182,7 +208,7 @@ with block:
|
|
182 |
|
183 |
|
184 |
|
185 |
-
ips = [fore, prompt,
|
186 |
generate_button.click(fn=process_generate, inputs=ips, outputs=[result_gallery])
|
187 |
|
188 |
|
|
|
68 |
|
69 |
return image
|
70 |
|
71 |
+
def resize_to_fit(max_size, original_size):
|
72 |
+
"""
|
73 |
+
Calculate the new size for an image to fit within max_size while maintaining the aspect ratio.
|
74 |
+
|
75 |
+
:param max_size: Maximum allowed size as a tuple (width, height).
|
76 |
+
:param original_size: Original size of the image as a tuple (width, height).
|
77 |
+
:return: New size as a tuple (new_width, new_height) that fits within max_size while maintaining the aspect ratio.
|
78 |
+
"""
|
79 |
+
original_width, original_height = original_size
|
80 |
+
max_width, max_height = max_size
|
81 |
+
|
82 |
+
# Calculate the scaling factor to maintain aspect ratio
|
83 |
+
width_ratio = max_width / original_width
|
84 |
+
height_ratio = max_height / original_height
|
85 |
+
scaling_factor = min(width_ratio, height_ratio)
|
86 |
+
|
87 |
+
# Calculate the new size while maintaining the aspect ratio
|
88 |
+
new_width = int(original_width * scaling_factor)
|
89 |
+
new_height = int(original_height * scaling_factor)
|
90 |
+
|
91 |
+
return new_width, new_height
|
92 |
+
|
93 |
+
|
94 |
+
def process_generate(fore, prompt, intensity, mode, refprompt, bg):
|
95 |
|
96 |
size = fore.size
|
97 |
+
image_width = size[0]
|
98 |
+
image_height = size[1]
|
99 |
+
|
100 |
if size[0]*size[1]<=(768*768):
|
101 |
gr.Warning("ℹ️ The input image resolution is low, it might lead to some deformation!")
|
102 |
|
103 |
if size[0]*size[1]>(1500*1500):
|
104 |
gr.Warning("ℹ️ The input image size is too big, I will lower it!")
|
105 |
+
image_width, image_height = resize_to_fit((1500,1500), (image_width, image_height))
|
106 |
+
|
107 |
|
108 |
|
109 |
+
forestr = image_to_base64(fore.convert("RGBA"))
|
110 |
data = {
|
111 |
"foreground_image64": forestr,
|
112 |
"prompt" : prompt,
|
|
|
208 |
|
209 |
|
210 |
|
211 |
+
ips = [fore, prompt, intensity, mode, refprompt, bg]
|
212 |
generate_button.click(fn=process_generate, inputs=ips, outputs=[result_gallery])
|
213 |
|
214 |
|