Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,61 +2,62 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import zipfile
|
| 4 |
from PIL import Image
|
| 5 |
-
from pathlib import Path
|
| 6 |
|
| 7 |
-
def
|
| 8 |
if not input_files:
|
| 9 |
-
return None,
|
| 10 |
-
|
| 11 |
output_folder = "processed_images"
|
| 12 |
os.makedirs(output_folder, exist_ok=True)
|
| 13 |
|
| 14 |
processed_paths = []
|
| 15 |
|
| 16 |
-
#
|
| 17 |
for i, file_path in enumerate(input_files, start=1):
|
| 18 |
img = Image.open(file_path)
|
| 19 |
-
|
| 20 |
-
# Define the new filename (e.g., image1.png)
|
| 21 |
new_name = f"image{i}.png"
|
| 22 |
save_path = os.path.join(output_folder, new_name)
|
| 23 |
-
|
| 24 |
-
# Save the image to the new path
|
| 25 |
img.save(save_path)
|
| 26 |
processed_paths.append(save_path)
|
| 27 |
|
| 28 |
-
# Create
|
| 29 |
-
zip_path = "
|
| 30 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 31 |
for file in processed_paths:
|
| 32 |
zipf.write(file, arcname=os.path.basename(file))
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
# --- Gradio UI Design ---
|
| 37 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 38 |
-
gr.Markdown("# 🖼️ Image Batch Processor")
|
| 39 |
-
gr.Markdown("Upload images
|
| 40 |
|
| 41 |
with gr.Row():
|
| 42 |
-
with gr.Column():
|
| 43 |
-
# Dropzone for multiple images
|
| 44 |
file_input = gr.File(
|
| 45 |
-
label="Upload Images",
|
| 46 |
file_count="multiple",
|
| 47 |
file_types=["image"]
|
| 48 |
)
|
| 49 |
-
process_btn = gr.Button("
|
| 50 |
|
| 51 |
-
with gr.Column():
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
# Connect the
|
| 56 |
process_btn.click(
|
| 57 |
-
fn=
|
| 58 |
inputs=file_input,
|
| 59 |
-
outputs=
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import os
|
| 3 |
import zipfile
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
+
def process_and_preview(input_files):
|
| 7 |
if not input_files:
|
| 8 |
+
return None, None
|
| 9 |
+
|
| 10 |
output_folder = "processed_images"
|
| 11 |
os.makedirs(output_folder, exist_ok=True)
|
| 12 |
|
| 13 |
processed_paths = []
|
| 14 |
|
| 15 |
+
# Process images
|
| 16 |
for i, file_path in enumerate(input_files, start=1):
|
| 17 |
img = Image.open(file_path)
|
|
|
|
|
|
|
| 18 |
new_name = f"image{i}.png"
|
| 19 |
save_path = os.path.join(output_folder, new_name)
|
|
|
|
|
|
|
| 20 |
img.save(save_path)
|
| 21 |
processed_paths.append(save_path)
|
| 22 |
|
| 23 |
+
# Create ZIP
|
| 24 |
+
zip_path = "processed_batch.zip"
|
| 25 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 26 |
for file in processed_paths:
|
| 27 |
zipf.write(file, arcname=os.path.basename(file))
|
| 28 |
|
| 29 |
+
# Return both the zip file and the list of paths for the Gallery preview
|
| 30 |
+
return zip_path, processed_paths
|
| 31 |
|
|
|
|
| 32 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 33 |
+
gr.Markdown("# 🖼️ Image Batch Processor (Gradio 6.9)")
|
| 34 |
+
gr.Markdown("Upload images, rename them, and download the ZIP. [GitHub](https://github.com)")
|
| 35 |
|
| 36 |
with gr.Row():
|
| 37 |
+
with gr.Column(scale=1):
|
|
|
|
| 38 |
file_input = gr.File(
|
| 39 |
+
label="Step 1: Upload Images",
|
| 40 |
file_count="multiple",
|
| 41 |
file_types=["image"]
|
| 42 |
)
|
| 43 |
+
process_btn = gr.Button("Step 2: Process & Zip", variant="primary")
|
| 44 |
|
| 45 |
+
with gr.Column(scale=2):
|
| 46 |
+
# The preview gallery
|
| 47 |
+
preview_gallery = gr.Gallery(
|
| 48 |
+
label="Image Preview",
|
| 49 |
+
columns=4,
|
| 50 |
+
height="auto",
|
| 51 |
+
object_fit="contain"
|
| 52 |
+
)
|
| 53 |
+
# The final download button/file
|
| 54 |
+
zip_output = gr.File(label="Step 3: Download ZIP")
|
| 55 |
|
| 56 |
+
# Connect the button to the function
|
| 57 |
process_btn.click(
|
| 58 |
+
fn=process_and_preview,
|
| 59 |
inputs=file_input,
|
| 60 |
+
outputs=[zip_output, preview_gallery]
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|