ssh-AI commited on
Commit
bfad80e
·
verified ·
1 Parent(s): 3c094b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -6,6 +6,7 @@ import cv2
6
  import numpy as np
7
  from fpdf import FPDF
8
  from streamlit_cropper import st_cropper
 
9
 
10
  # --- Set Page Config ---
11
  st.set_page_config(page_title="Smart ID Scanner", page_icon="🪪", layout="centered")
@@ -78,22 +79,35 @@ if uploaded_files:
78
  if st.button("Generate PDF 📄"):
79
  with st.spinner('Generating PDF...'):
80
  pdf = FPDF()
 
81
  for img in cropped_images:
82
  temp_path = f"/tmp/{np.random.randint(0,10000)}.jpg"
83
  img.save(temp_path, format="JPEG")
 
84
  pdf.add_page()
85
  pdf.image(temp_path, x=10, y=10, w=190)
86
- os.remove(temp_path)
87
- pdf_output = BytesIO()
88
- pdf.output(pdf_output)
89
- st.download_button(label="📥 Download PDF", data=pdf_output.getvalue(), file_name="scanned_document.pdf", mime="application/pdf")
90
-
91
- # Save as Images
 
 
 
 
 
 
 
92
  with col2:
93
- if st.button("Save as JPG/PNG 📷"):
94
  with st.spinner('Saving Images...'):
95
- for idx, img in enumerate(cropped_images):
96
- img_output = BytesIO()
97
- img.save(img_output, format='PNG')
98
- st.download_button(label=f"📥 Download Image {idx+1}", data=img_output.getvalue(), file_name=f"scanned_image_{idx+1}.png", mime="image/png")
99
-
 
 
 
 
 
6
  import numpy as np
7
  from fpdf import FPDF
8
  from streamlit_cropper import st_cropper
9
+ import zipfile
10
 
11
  # --- Set Page Config ---
12
  st.set_page_config(page_title="Smart ID Scanner", page_icon="🪪", layout="centered")
 
79
  if st.button("Generate PDF 📄"):
80
  with st.spinner('Generating PDF...'):
81
  pdf = FPDF()
82
+ temp_image_paths = []
83
  for img in cropped_images:
84
  temp_path = f"/tmp/{np.random.randint(0,10000)}.jpg"
85
  img.save(temp_path, format="JPEG")
86
+ temp_image_paths.append(temp_path)
87
  pdf.add_page()
88
  pdf.image(temp_path, x=10, y=10, w=190)
89
+
90
+ pdf_output_path = "/tmp/scanned_document.pdf"
91
+ pdf.output(pdf_output_path)
92
+
93
+ with open(pdf_output_path, "rb") as f:
94
+ st.download_button(label="📥 Download PDF", data=f, file_name="scanned_document.pdf", mime="application/pdf")
95
+
96
+ # Clean up
97
+ for path in temp_image_paths:
98
+ os.remove(path)
99
+ os.remove(pdf_output_path)
100
+
101
+ # Save all as Images in ZIP
102
  with col2:
103
+ if st.button("Save All as Images 📷"):
104
  with st.spinner('Saving Images...'):
105
+ zip_buffer = BytesIO()
106
+ with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED) as zip_file:
107
+ for idx, img in enumerate(cropped_images):
108
+ img_bytes = BytesIO()
109
+ img.save(img_bytes, format='PNG')
110
+ img_bytes.seek(0)
111
+ zip_file.writestr(f"scanned_image_{idx+1}.png", img_bytes.read())
112
+ zip_buffer.seek(0)
113
+ st.download_button(label="📥 Download All Images (ZIP)", data=zip_buffer, file_name="scanned_images.zip", mime="application/zip")