Update app.py
Browse files
app.py
CHANGED
|
@@ -54,7 +54,14 @@ def generate_html_snippet(image_path_prefix):
|
|
| 54 |
|
| 55 |
return html
|
| 56 |
|
| 57 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""Main processing function"""
|
| 59 |
if input_img is None:
|
| 60 |
return None, None, "Please upload an image"
|
|
@@ -65,9 +72,13 @@ def process_image(input_img, input_name):
|
|
| 65 |
processed_paths = [] # Initialize the list outside try block
|
| 66 |
|
| 67 |
try:
|
| 68 |
-
# Get
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Convert to PIL Image if needed
|
| 72 |
if isinstance(input_img, np.ndarray):
|
| 73 |
img = Image.fromarray(input_img)
|
|
@@ -80,8 +91,9 @@ def process_image(input_img, input_name):
|
|
| 80 |
|
| 81 |
# Process each size
|
| 82 |
for width, height in SIZES:
|
| 83 |
-
|
| 84 |
output_path = os.path.join(temp_dir, f"{base_filename}-{width}x{height}.jpg")
|
|
|
|
| 85 |
resized.save(output_path, "JPEG", quality=90)
|
| 86 |
processed_paths.append(output_path)
|
| 87 |
|
|
@@ -90,8 +102,9 @@ def process_image(input_img, input_name):
|
|
| 90 |
for path in processed_paths:
|
| 91 |
zf.write(path, os.path.basename(path))
|
| 92 |
|
| 93 |
-
# Generate HTML snippet using
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
return zip_path, html_snippet, "Processing completed successfully!"
|
| 97 |
|
|
@@ -125,6 +138,8 @@ with gr.Blocks(title="Responsive Image Generator") as app:
|
|
| 125 |
You'll receive:
|
| 126 |
1. A ZIP file containing all sized versions
|
| 127 |
2. HTML code snippet with proper srcset attributes
|
|
|
|
|
|
|
| 128 |
""")
|
| 129 |
|
| 130 |
with gr.Row():
|
|
@@ -136,9 +151,10 @@ with gr.Blocks(title="Responsive Image Generator") as app:
|
|
| 136 |
show_label=True
|
| 137 |
)
|
| 138 |
with gr.Row():
|
| 139 |
-
|
| 140 |
-
label="
|
| 141 |
-
placeholder="
|
|
|
|
| 142 |
)
|
| 143 |
process_btn = gr.Button("Process Image")
|
| 144 |
|
|
@@ -160,14 +176,15 @@ with gr.Blocks(title="Responsive Image Generator") as app:
|
|
| 160 |
input_image.upload(
|
| 161 |
fn=handle_upload,
|
| 162 |
inputs=[input_image],
|
| 163 |
-
outputs=[
|
| 164 |
)
|
| 165 |
|
| 166 |
# Process button click
|
| 167 |
process_btn.click(
|
| 168 |
fn=process_image,
|
| 169 |
-
inputs=[input_image,
|
| 170 |
outputs=[output_zip, output_html, output_message]
|
| 171 |
)
|
| 172 |
|
| 173 |
-
app
|
|
|
|
|
|
| 54 |
|
| 55 |
return html
|
| 56 |
|
| 57 |
+
def get_path_components(filepath):
|
| 58 |
+
"""Split filepath into directory and filename"""
|
| 59 |
+
dirpath = os.path.dirname(filepath)
|
| 60 |
+
filename = os.path.basename(filepath)
|
| 61 |
+
base_filename = os.path.splitext(filename)[0]
|
| 62 |
+
return dirpath, base_filename
|
| 63 |
+
|
| 64 |
+
def process_image(input_img, input_path):
|
| 65 |
"""Main processing function"""
|
| 66 |
if input_img is None:
|
| 67 |
return None, None, "Please upload an image"
|
|
|
|
| 72 |
processed_paths = [] # Initialize the list outside try block
|
| 73 |
|
| 74 |
try:
|
| 75 |
+
# Get path components
|
| 76 |
+
if input_path and input_path.strip():
|
| 77 |
+
dirpath, base_filename = get_path_components(input_path.strip())
|
| 78 |
+
else:
|
| 79 |
+
# If no path provided, use default
|
| 80 |
+
dirpath, base_filename = "", "image"
|
| 81 |
+
|
| 82 |
# Convert to PIL Image if needed
|
| 83 |
if isinstance(input_img, np.ndarray):
|
| 84 |
img = Image.fromarray(input_img)
|
|
|
|
| 91 |
|
| 92 |
# Process each size
|
| 93 |
for width, height in SIZES:
|
| 94 |
+
# For zip file, only use filename without path
|
| 95 |
output_path = os.path.join(temp_dir, f"{base_filename}-{width}x{height}.jpg")
|
| 96 |
+
resized = resize_image(img, (width, height))
|
| 97 |
resized.save(output_path, "JPEG", quality=90)
|
| 98 |
processed_paths.append(output_path)
|
| 99 |
|
|
|
|
| 102 |
for path in processed_paths:
|
| 103 |
zf.write(path, os.path.basename(path))
|
| 104 |
|
| 105 |
+
# Generate HTML snippet using full path
|
| 106 |
+
full_path = dirpath + ("/" if dirpath else "") + base_filename
|
| 107 |
+
html_snippet = generate_html_snippet(full_path)
|
| 108 |
|
| 109 |
return zip_path, html_snippet, "Processing completed successfully!"
|
| 110 |
|
|
|
|
| 138 |
You'll receive:
|
| 139 |
1. A ZIP file containing all sized versions
|
| 140 |
2. HTML code snippet with proper srcset attributes
|
| 141 |
+
|
| 142 |
+
Optional: Specify the full path where images will be stored (e.g., 'assets/images/about/mock-up.png')
|
| 143 |
""")
|
| 144 |
|
| 145 |
with gr.Row():
|
|
|
|
| 151 |
show_label=True
|
| 152 |
)
|
| 153 |
with gr.Row():
|
| 154 |
+
input_path = gr.Textbox(
|
| 155 |
+
label="Image Path (optional)",
|
| 156 |
+
placeholder="e.g., assets/images/about/mock-up.png",
|
| 157 |
+
value=""
|
| 158 |
)
|
| 159 |
process_btn = gr.Button("Process Image")
|
| 160 |
|
|
|
|
| 176 |
input_image.upload(
|
| 177 |
fn=handle_upload,
|
| 178 |
inputs=[input_image],
|
| 179 |
+
outputs=[input_path]
|
| 180 |
)
|
| 181 |
|
| 182 |
# Process button click
|
| 183 |
process_btn.click(
|
| 184 |
fn=process_image,
|
| 185 |
+
inputs=[input_image, input_path],
|
| 186 |
outputs=[output_zip, output_html, output_message]
|
| 187 |
)
|
| 188 |
|
| 189 |
+
# Launch the app with share=True for public access
|
| 190 |
+
app.launch(share=True)
|