Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,26 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
import
|
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 |
-
|
18 |
-
|
19 |
-
"
|
20 |
-
|
21 |
-
"
|
22 |
-
|
23 |
-
|
24 |
-
|
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 |
|
@@ -36,23 +28,23 @@ uploaded_images = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"],
|
|
36 |
|
37 |
if uploaded_images:
|
38 |
num_images = len(uploaded_images)
|
39 |
-
if num_images > 0: #
|
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 #
|
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: #
|
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) #
|
57 |
else:
|
58 |
st.write("Please upload images to continue")
|
|
|
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):
|
12 |
pdf_name = "output.pdf"
|
13 |
|
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)")
|
26 |
|
|
|
28 |
|
29 |
if uploaded_images:
|
30 |
num_images = len(uploaded_images)
|
31 |
+
if num_images > 0: # Check if any image is uploaded
|
32 |
images_with_sequence = []
|
33 |
|
34 |
for i, image_file in enumerate(uploaded_images):
|
35 |
st.image(image_file, width=250)
|
36 |
sequence_options = [str(j) for j in range(1, num_images + 1)]
|
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)
|
44 |
+
if pdf_name: # Check if PDF is generated successfully
|
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")
|