Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -21,34 +21,37 @@ ASSETS = {
|
|
21 |
"logo": "logo.png"
|
22 |
}
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def resize_image_to_short_edge(image_path, max_short_edge=1280):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
if width
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
resized_img.save(buffer, format=img.format)
|
47 |
-
buffer.seek(0)
|
48 |
-
return buffer
|
49 |
|
50 |
def validate_assets():
|
51 |
-
|
52 |
missing = []
|
53 |
for asset_type in ["top", "bottom"]:
|
54 |
if not os.path.exists(ASSETS[asset_type]["reference_image"]):
|
@@ -64,7 +67,7 @@ def generate_and_wait_for_image(
|
|
64 |
garment_type: str,
|
65 |
progress=gr.Progress()
|
66 |
):
|
67 |
-
|
68 |
# Validate assets first
|
69 |
try:
|
70 |
validate_assets()
|
@@ -90,10 +93,16 @@ def generate_and_wait_for_image(
|
|
90 |
|
91 |
# Prepare all required files
|
92 |
files = {
|
93 |
-
'input_image': resize_image_to_short_edge(input_image),
|
94 |
'logo_image': open(ASSETS["logo"], 'rb'),
|
95 |
'reference_image': open(ASSETS[garment_type]["reference_image"], 'rb')
|
96 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
data = {
|
98 |
'reference_image_type': (f'{garment_type}'),
|
99 |
'output_format': (None, "png") # Hardcoded to PNG
|
|
|
21 |
"logo": "logo.png"
|
22 |
}
|
23 |
|
24 |
+
def needs_resizing(image_path, max_short_edge=1280):
|
25 |
+
# Check if image needs resizing (returns True if short edge > max_short_edge)
|
26 |
+
with Image.open(image_path) as img:
|
27 |
+
width, height = img.size
|
28 |
+
return min(width, height) > max_short_edge
|
29 |
+
|
30 |
def resize_image_to_short_edge(image_path, max_short_edge=1280):
|
31 |
+
# Resize image so the short edge is at most max_short_edge pixels
|
32 |
+
with Image.open(image_path) as img:
|
33 |
+
width, height = img.size
|
34 |
+
format = img.format
|
35 |
+
|
36 |
+
# Determine scaling factor
|
37 |
+
if width < height:
|
38 |
+
scaling_factor = max_short_edge / width
|
39 |
+
else:
|
40 |
+
scaling_factor = max_short_edge / height
|
41 |
+
|
42 |
+
# Calculate new dimensions and resize
|
43 |
+
new_width = int(width * scaling_factor)
|
44 |
+
new_height = int(height * scaling_factor)
|
45 |
+
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
|
46 |
+
|
47 |
+
# Save to bytes buffer
|
48 |
+
buffer = io.BytesIO()
|
49 |
+
resized_img.save(buffer, format=format)
|
50 |
+
buffer.seek(0)
|
51 |
+
return buffer
|
|
|
|
|
|
|
52 |
|
53 |
def validate_assets():
|
54 |
+
# Check if all required asset files exist
|
55 |
missing = []
|
56 |
for asset_type in ["top", "bottom"]:
|
57 |
if not os.path.exists(ASSETS[asset_type]["reference_image"]):
|
|
|
67 |
garment_type: str,
|
68 |
progress=gr.Progress()
|
69 |
):
|
70 |
+
# Make POST request and automatically poll for the result image
|
71 |
# Validate assets first
|
72 |
try:
|
73 |
validate_assets()
|
|
|
93 |
|
94 |
# Prepare all required files
|
95 |
files = {
|
|
|
96 |
'logo_image': open(ASSETS["logo"], 'rb'),
|
97 |
'reference_image': open(ASSETS[garment_type]["reference_image"], 'rb')
|
98 |
}
|
99 |
+
|
100 |
+
# Only resize input_image if needed
|
101 |
+
if needs_resizing(input_image):
|
102 |
+
files['input_image'] = resize_image_to_short_edge(input_image)
|
103 |
+
else:
|
104 |
+
files['input_image'] = open(input_image, 'rb')
|
105 |
+
|
106 |
data = {
|
107 |
'reference_image_type': (f'{garment_type}'),
|
108 |
'output_format': (None, "png") # Hardcoded to PNG
|