Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import zipfile
4
+ from PIL import Image
5
+ from pathlib import Path
6
+
7
+ def process_images(input_files):
8
+ if not input_files:
9
+ return None, "No files uploaded."
10
+
11
+ output_folder = "processed_images"
12
+ os.makedirs(output_folder, exist_ok=True)
13
+
14
+ processed_paths = []
15
+
16
+ # This logic replaces your JS loop: it renames files image1, image2...
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 the ZIP file
29
+ zip_path = "renamed_images.zip"
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
+ return zip_path
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 to rename them sequentially and download a ZIP file. [View GitHub](https://github.com)")
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("Rewrite & Zip Files", variant="primary")
50
+
51
+ with gr.Column():
52
+ # This appears when processing is done
53
+ file_output = gr.File(label="Download Processed ZIP")
54
+
55
+ # Connect the logic
56
+ process_btn.click(
57
+ fn=process_images,
58
+ inputs=file_input,
59
+ outputs=file_output
60
+ )
61
+
62
+ if __name__ == "__main__":
63
+ demo.launch()