Update app.py
Browse files
app.py
CHANGED
|
@@ -61,6 +61,7 @@ def process_image(input_image):
|
|
| 61 |
# Create temporary directory for processed images
|
| 62 |
temp_dir = tempfile.mkdtemp()
|
| 63 |
zip_path = os.path.join(temp_dir, "responsive_images.zip")
|
|
|
|
| 64 |
|
| 65 |
try:
|
| 66 |
# Open and process image
|
|
@@ -71,7 +72,6 @@ def process_image(input_image):
|
|
| 71 |
img = img.convert('RGB')
|
| 72 |
|
| 73 |
# Process each size
|
| 74 |
-
processed_paths = []
|
| 75 |
for width, height in SIZES:
|
| 76 |
resized = resize_image(img, (width, height))
|
| 77 |
output_path = os.path.join(temp_dir, f"image-{width}x{height}.jpg")
|
|
@@ -89,6 +89,11 @@ def process_image(input_image):
|
|
| 89 |
return zip_path, html_snippet, "Processing completed successfully!"
|
| 90 |
|
| 91 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return None, None, f"Error processing image: {str(e)}"
|
| 93 |
|
| 94 |
finally:
|
|
@@ -98,7 +103,12 @@ def process_image(input_image):
|
|
| 98 |
os.remove(path)
|
| 99 |
except:
|
| 100 |
pass
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Create Gradio interface
|
| 103 |
with gr.Blocks(title="Responsive Image Generator") as app:
|
| 104 |
gr.Markdown("""
|
|
|
|
| 61 |
# Create temporary directory for processed images
|
| 62 |
temp_dir = tempfile.mkdtemp()
|
| 63 |
zip_path = os.path.join(temp_dir, "responsive_images.zip")
|
| 64 |
+
processed_paths = [] # Initialize the list outside try block
|
| 65 |
|
| 66 |
try:
|
| 67 |
# Open and process image
|
|
|
|
| 72 |
img = img.convert('RGB')
|
| 73 |
|
| 74 |
# Process each size
|
|
|
|
| 75 |
for width, height in SIZES:
|
| 76 |
resized = resize_image(img, (width, height))
|
| 77 |
output_path = os.path.join(temp_dir, f"image-{width}x{height}.jpg")
|
|
|
|
| 89 |
return zip_path, html_snippet, "Processing completed successfully!"
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
if os.path.exists(zip_path):
|
| 93 |
+
try:
|
| 94 |
+
os.remove(zip_path)
|
| 95 |
+
except:
|
| 96 |
+
pass
|
| 97 |
return None, None, f"Error processing image: {str(e)}"
|
| 98 |
|
| 99 |
finally:
|
|
|
|
| 103 |
os.remove(path)
|
| 104 |
except:
|
| 105 |
pass
|
| 106 |
+
# Try to remove the temp directory if it's empty
|
| 107 |
+
try:
|
| 108 |
+
os.rmdir(temp_dir)
|
| 109 |
+
except:
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
# Create Gradio interface
|
| 113 |
with gr.Blocks(title="Responsive Image Generator") as app:
|
| 114 |
gr.Markdown("""
|