ChBysk commited on
Commit
f733c5e
·
verified ·
1 Parent(s): 51bab06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -48
app.py CHANGED
@@ -1,64 +1,73 @@
1
  import gradio as gr
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__":
64
- demo.launch()
 
 
1
  import gradio as gr
2
  import os
3
+ import base64
4
  from PIL import Image
5
+ from io import BytesIO
6
 
7
+ def image_to_base64(img):
8
+ buffered = BytesIO()
9
+ # Downscale for mobile brochure feel (max width 600px)
10
+ img.thumbnail((600, 600))
11
+ img.save(buffered, format="PNG")
12
+ return base64.b64encode(buffered.getvalue()).decode()
13
+
14
+ def generate_brochure(image_files, info_file):
15
+ if not image_files or not info_file:
16
+ return None
17
 
18
+ # 1. Read and split the info text
19
+ with open(info_file.name, 'r') as f:
20
+ text_content = f.read().split('\n\n') # Assumes double new-line separates info
21
 
22
+ md_content = "# Project Brochure\n\n"
23
 
24
+ # 2. Logic to group 3 images and 3 text blocks
25
+ # We loop through images in steps of 3
26
+ for i in range(0, len(image_files), 3):
27
+ md_content += '<div style="max-width: 400px; margin: auto; border: 1px solid #ccc; padding: 10px;">\n\n'
28
+ md_content += f"## Page { (i//3) + 1 }\n\n"
29
+
30
+ # Get the next 3 images and next 3 text blocks
31
+ current_images = image_files[i:i+3]
32
+ current_texts = text_content[i:i+3]
33
+
34
+ for img_path, txt in zip(current_images, current_texts):
35
+ img = Image.open(img_path)
36
+ b64 = image_to_base64(img)
37
 
38
+ # Add to Markdown with HTML for mobile-style sizing
39
+ md_content += f'<img src="data:image/png;base64,{b64}" style="width:100%; border-radius:8px;">\n\n'
40
+ md_content += f"{txt}\n\n---\n\n"
41
+
42
+ md_content += '</div>\n<div style="page-break-after: always;"></div>\n\n'
43
+
44
+ # 3. Save the result
45
+ output_path = "brochure.md"
46
+ with open(output_path, "w", encoding="utf-8") as f:
47
+ f.write(md_content)
48
+
49
+ return output_path
50
 
51
+ # --- Gradio UI ---
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("# 📱 Mobile Brochure Generator")
54
+ gr.Markdown("Upload images and an `info.txt` file to generate a self-contained Markdown brochure.")
55
 
56
  with gr.Row():
57
+ with gr.Column():
58
+ img_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
59
+ txt_input = gr.File(label="Upload info.txt", file_types=[".txt"])
60
+ gen_btn = gr.Button("Generate Brochure", variant="primary")
 
 
 
61
 
62
+ with gr.Column():
63
+ out_file = gr.File(label="Download .md Brochure")
 
 
 
 
 
 
 
 
64
 
65
+ gen_btn.click(
66
+ fn=generate_brochure,
67
+ inputs=[img_input, txt_input],
68
+ outputs=out_file
 
69
  )
70
 
71
  if __name__ == "__main__":
72
+ # Note: Using the new Gradio 6.0+ launch style
73
+ demo.launch(theme=gr.themes.Soft())