engrharis commited on
Commit
a1f3ddf
·
verified ·
1 Parent(s): d8f0819

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -49
app.py CHANGED
@@ -1,75 +1,58 @@
1
  import streamlit as st
2
  from PIL import Image
3
- import pytesseract # For basic text extraction (optional)
4
- import pdfkit # For PDF generation
5
- import os # For temporary file handling
6
 
7
  # Function to process a single image with user-provided sequence number
8
  def process_image(image_file, sequence_number):
9
  image = Image.open(image_file)
10
-
11
- # Optionally extract text using Tesseract (if needed for layout)
12
- # text = pytesseract.image_to_string(image) # May require Tesseract installation
13
-
14
- # Consider techniques for layout analysis and image manipulation (if complex)
15
-
16
- return image, sequence_number # Return the image and its sequence number
17
 
18
  # Function to generate the PDF
19
  def generate_pdf(images_with_sequence):
20
  pdf_name = "output.pdf"
21
 
22
- # Create a temporary directory to store intermediate images (if needed)
23
- # temp_dir = os.path.join(os.getcwd(), "temp_images")
24
- # os.makedirs(temp_dir, exist_ok=True)
25
-
26
- # Sort images by sequence number
27
  images_with_sequence.sort(key=lambda x: x[1])
28
 
 
29
  pdf_options = {
30
  "margin-top": "0.5in",
31
  "margin-right": "0.5in",
32
  "margin-bottom": "0.5in",
33
  "margin-left": "0.5in",
34
  "encoding": "UTF-8",
35
- "dpi": 300, # Adjust as needed
36
  }
37
-
38
- # Generate PDF content (consider layout strategies if necessary)
39
- pdfkit.from_file(
40
- [image[0] for image in images_with_sequence],
41
- pdf_name,
42
- options=pdf_options,
43
- )
44
-
45
- # Clean up temporary files (if used)
46
- # for filename in os.listdir(temp_dir):
47
- # os.remove(os.path.join(temp_dir, filename))
48
-
49
- return pdf_name
50
 
51
  st.title("Image to PDF Converter (Multiple Images)")
52
 
53
  uploaded_images = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple=True)
54
 
55
  if uploaded_images:
56
- images_with_sequence = []
57
-
58
- for image_file in uploaded_images:
59
- st.image(image_file, width=250) # Display uploaded images
60
-
61
- sequence_options = [str(i) for i in range(1, len(uploaded_images) + 1)]
62
- sequence_number = st.selectbox("Sequence Number", sequence_options)
63
-
64
- processed_image, processed_sequence = process_image(image_file, int(sequence_number))
65
- images_with_sequence.append((processed_image, processed_sequence))
66
-
67
- if st.button("Generate PDF"):
68
- pdf_name = generate_pdf(images_with_sequence)
69
- st.success(f"PDF generated! Download: {pdf_name}")
70
-
71
- with open(pdf_name, "rb") as pdf_file:
72
- st.download_button("Download PDF", pdf_file, file_name=pdf_name)
73
-
74
- # Delete the generated PDF after download (optional)
75
- # os.remove(pdf_name)
 
 
1
  import streamlit as st
2
  from PIL import Image
3
+ import pdfkit
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
  image = Image.open(image_file)
9
+ return image, sequence_number
 
 
 
 
 
 
10
 
11
  # Function to generate the PDF
12
  def generate_pdf(images_with_sequence):
13
  pdf_name = "output.pdf"
14
 
 
 
 
 
 
15
  images_with_sequence.sort(key=lambda x: x[1])
16
 
17
+ config = pdfkit.configuration(wkhtmltopdf=r'/usr/bin/wkhtmltopdf') #provide your wkhtmltopdf path here
18
  pdf_options = {
19
  "margin-top": "0.5in",
20
  "margin-right": "0.5in",
21
  "margin-bottom": "0.5in",
22
  "margin-left": "0.5in",
23
  "encoding": "UTF-8",
24
+ "dpi": 300,
25
  }
26
+ try:
27
+ pdfkit.from_file([image[0] for image in images_with_sequence], pdf_name, options=pdf_options, configuration=config)
28
+ return pdf_name
29
+ except Exception as e:
30
+ st.error(f"Error generating PDF: {e}")
31
+ return None
 
 
 
 
 
 
 
32
 
33
  st.title("Image to PDF Converter (Multiple Images)")
34
 
35
  uploaded_images = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple=True)
36
 
37
  if uploaded_images:
38
+ num_images = len(uploaded_images)
39
+ if num_images > 0: #check if any image is uploaded
40
+ images_with_sequence = []
41
+
42
+ for i, image_file in enumerate(uploaded_images):
43
+ st.image(image_file, width=250)
44
+ sequence_options = [str(j) for j in range(1, num_images + 1)]
45
+ default_index = i #set default selection of sequence as the order of upload
46
+ sequence_number = st.selectbox(f"Sequence Number for Image {i+1}", sequence_options, index = default_index)
47
+ processed_image, processed_sequence = process_image(image_file, int(sequence_number))
48
+ images_with_sequence.append((processed_image, processed_sequence))
49
+
50
+ if st.button("Generate PDF"):
51
+ pdf_name = generate_pdf(images_with_sequence)
52
+ if pdf_name: #check if pdf is generated successfully
53
+ st.success(f"PDF generated! Download: {pdf_name}")
54
+ with open(pdf_name, "rb") as pdf_file:
55
+ st.download_button("Download PDF", pdf_file, file_name=pdf_name)
56
+ os.remove(pdf_name) #remove the file after download
57
+ else:
58
+ st.write("Please upload images to continue")