Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,11 @@ def resize_image(img, target_size):
|
|
| 26 |
img_copy = img.copy()
|
| 27 |
return img_copy.resize(target_size, PIL.Image.Resampling.LANCZOS)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def generate_html_snippet(image_path_prefix):
|
| 30 |
"""Generate HTML code snippet with srcset"""
|
| 31 |
srcset_items = [f"{image_path_prefix}-{w}x{h}.jpg {w}w" for w, h in SIZES]
|
|
@@ -54,7 +59,7 @@ def generate_html_snippet(image_path_prefix):
|
|
| 54 |
|
| 55 |
return html
|
| 56 |
|
| 57 |
-
def process_image(input_image):
|
| 58 |
"""Main processing function"""
|
| 59 |
if input_image is None:
|
| 60 |
return None, None, "Please upload an image"
|
|
@@ -72,10 +77,13 @@ def process_image(input_image):
|
|
| 72 |
if img.mode != 'RGB':
|
| 73 |
img = img.convert('RGB')
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
# Process each size
|
| 76 |
for width, height in SIZES:
|
| 77 |
resized = resize_image(img, (width, height))
|
| 78 |
-
output_path = os.path.join(temp_dir, f"
|
| 79 |
resized.save(output_path, "JPEG", quality=90)
|
| 80 |
processed_paths.append(output_path)
|
| 81 |
|
|
@@ -84,8 +92,8 @@ def process_image(input_image):
|
|
| 84 |
for path in processed_paths:
|
| 85 |
zf.write(path, os.path.basename(path))
|
| 86 |
|
| 87 |
-
# Generate HTML snippet
|
| 88 |
-
html_snippet = generate_html_snippet("images/
|
| 89 |
|
| 90 |
return zip_path, html_snippet, "Processing completed successfully!"
|
| 91 |
|
|
@@ -125,7 +133,8 @@ with gr.Blocks(title="Responsive Image Generator") as app:
|
|
| 125 |
with gr.Column():
|
| 126 |
input_image = gr.Image(
|
| 127 |
label="Upload Original Image",
|
| 128 |
-
type="numpy" #
|
|
|
|
| 129 |
)
|
| 130 |
process_btn = gr.Button("Process Image")
|
| 131 |
|
|
@@ -139,8 +148,9 @@ with gr.Blocks(title="Responsive Image Generator") as app:
|
|
| 139 |
|
| 140 |
process_btn.click(
|
| 141 |
fn=process_image,
|
| 142 |
-
inputs=[input_image],
|
| 143 |
outputs=[output_zip, output_html, output_message]
|
| 144 |
)
|
| 145 |
|
|
|
|
| 146 |
app.launch()
|
|
|
|
| 26 |
img_copy = img.copy()
|
| 27 |
return img_copy.resize(target_size, PIL.Image.Resampling.LANCZOS)
|
| 28 |
|
| 29 |
+
def get_base_filename(original_path):
|
| 30 |
+
"""Extract original filename without extension"""
|
| 31 |
+
base_name = os.path.basename(original_path)
|
| 32 |
+
return os.path.splitext(base_name)[0]
|
| 33 |
+
|
| 34 |
def generate_html_snippet(image_path_prefix):
|
| 35 |
"""Generate HTML code snippet with srcset"""
|
| 36 |
srcset_items = [f"{image_path_prefix}-{w}x{h}.jpg {w}w" for w, h in SIZES]
|
|
|
|
| 59 |
|
| 60 |
return html
|
| 61 |
|
| 62 |
+
def process_image(input_image, original_filename):
|
| 63 |
"""Main processing function"""
|
| 64 |
if input_image is None:
|
| 65 |
return None, None, "Please upload an image"
|
|
|
|
| 77 |
if img.mode != 'RGB':
|
| 78 |
img = img.convert('RGB')
|
| 79 |
|
| 80 |
+
# Get base filename without extension
|
| 81 |
+
base_filename = os.path.splitext(original_filename)[0]
|
| 82 |
+
|
| 83 |
# Process each size
|
| 84 |
for width, height in SIZES:
|
| 85 |
resized = resize_image(img, (width, height))
|
| 86 |
+
output_path = os.path.join(temp_dir, f"{base_filename}-{width}x{height}.jpg")
|
| 87 |
resized.save(output_path, "JPEG", quality=90)
|
| 88 |
processed_paths.append(output_path)
|
| 89 |
|
|
|
|
| 92 |
for path in processed_paths:
|
| 93 |
zf.write(path, os.path.basename(path))
|
| 94 |
|
| 95 |
+
# Generate HTML snippet using original filename
|
| 96 |
+
html_snippet = generate_html_snippet(f"images/{base_filename}")
|
| 97 |
|
| 98 |
return zip_path, html_snippet, "Processing completed successfully!"
|
| 99 |
|
|
|
|
| 133 |
with gr.Column():
|
| 134 |
input_image = gr.Image(
|
| 135 |
label="Upload Original Image",
|
| 136 |
+
type="numpy", # Using numpy type for better compatibility
|
| 137 |
+
source="upload" # Ensure we only accept uploads
|
| 138 |
)
|
| 139 |
process_btn = gr.Button("Process Image")
|
| 140 |
|
|
|
|
| 148 |
|
| 149 |
process_btn.click(
|
| 150 |
fn=process_image,
|
| 151 |
+
inputs=[input_image, input_image], # Pass the image twice to get both data and filename
|
| 152 |
outputs=[output_zip, output_html, output_message]
|
| 153 |
)
|
| 154 |
|
| 155 |
+
# Launch the app with share=True for public access
|
| 156 |
app.launch()
|