engrharis commited on
Commit
9eeb86b
·
verified ·
1 Parent(s): 2265c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -1,11 +1,16 @@
1
  import streamlit as st
2
  from PIL import Image
3
  from fpdf import FPDF
 
4
 
5
  # Function to process a single image with user-provided sequence number
6
  def process_image(image_file, sequence_number):
7
- image = Image.open(image_file)
8
- return image, sequence_number
 
 
 
 
9
 
10
  # Function to generate the PDF using fpdf
11
  def generate_pdf(images_with_sequence):
@@ -14,12 +19,16 @@ def generate_pdf(images_with_sequence):
14
  images_with_sequence.sort(key=lambda x: x[1]) # Sort by sequence number
15
 
16
  pdf = FPDF()
17
- for image, _ in images_with_sequence:
18
- image.save("temp_image.jpg")
19
  pdf.add_page()
20
- pdf.image("temp_image.jpg", x=10, y=10, w=190)
21
 
22
- pdf.output(pdf_name)
 
 
 
 
23
  return pdf_name
24
 
25
  st.title("Image to PDF Converter (Multiple Images)")
@@ -37,7 +46,8 @@ if uploaded_images:
37
  default_index = i # Set default selection of sequence as the order of upload
38
  sequence_number = st.selectbox(f"Sequence Number for Image {i+1}", sequence_options, index=default_index)
39
  processed_image, processed_sequence = process_image(image_file, int(sequence_number))
40
- images_with_sequence.append((processed_image, processed_sequence))
 
41
 
42
  if st.button("Generate PDF"):
43
  pdf_name = generate_pdf(images_with_sequence)
@@ -45,6 +55,9 @@ if uploaded_images:
45
  st.success(f"PDF generated! Download: {pdf_name}")
46
  with open(pdf_name, "rb") as pdf_file:
47
  st.download_button("Download PDF", pdf_file, file_name=pdf_name)
 
 
 
48
  os.remove(pdf_name) # Remove the file after download
49
  else:
50
- st.write("Please upload images to continue")
 
1
  import streamlit as st
2
  from PIL import Image
3
  from fpdf import FPDF
4
+ import os
5
 
6
  # Function to process a single image with user-provided sequence number
7
  def process_image(image_file, sequence_number):
8
+ try:
9
+ image = Image.open(image_file)
10
+ return image, sequence_number
11
+ except Exception as e:
12
+ st.error(f"Failed to open image: {e}")
13
+ return None, None
14
 
15
  # Function to generate the PDF using fpdf
16
  def generate_pdf(images_with_sequence):
 
19
  images_with_sequence.sort(key=lambda x: x[1]) # Sort by sequence number
20
 
21
  pdf = FPDF()
22
+ for i, (image, _) in enumerate(images_with_sequence):
23
+ image.save(f"temp_image_{i}.jpg")
24
  pdf.add_page()
25
+ pdf.image(f"temp_image_{i}.jpg", x=10, y=10, w=190)
26
 
27
+ try:
28
+ pdf.output(pdf_name)
29
+ except Exception as e:
30
+ st.error(f"Failed to generate PDF: {e}")
31
+ return None
32
  return pdf_name
33
 
34
  st.title("Image to PDF Converter (Multiple Images)")
 
46
  default_index = i # Set default selection of sequence as the order of upload
47
  sequence_number = st.selectbox(f"Sequence Number for Image {i+1}", sequence_options, index=default_index)
48
  processed_image, processed_sequence = process_image(image_file, int(sequence_number))
49
+ if processed_image:
50
+ images_with_sequence.append((processed_image, processed_sequence))
51
 
52
  if st.button("Generate PDF"):
53
  pdf_name = generate_pdf(images_with_sequence)
 
55
  st.success(f"PDF generated! Download: {pdf_name}")
56
  with open(pdf_name, "rb") as pdf_file:
57
  st.download_button("Download PDF", pdf_file, file_name=pdf_name)
58
+ for i in range(len(images_with_sequence)):
59
+ if os.path.exists(f"temp_image_{i}.jpg"):
60
+ os.remove(f"temp_image_{i}.jpg")
61
  os.remove(pdf_name) # Remove the file after download
62
  else:
63
+ st.write("Please upload images to continue")