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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)