Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Merge Images to PDF", layout="centered")
|
7 |
+
st.title("🖼️➡️📄 Merge Multiple Images into a PDF")
|
8 |
+
|
9 |
+
uploaded_images = st.file_uploader(
|
10 |
+
"Upload multiple images (JPG, PNG, etc.)",
|
11 |
+
type=["png", "jpg", "jpeg"],
|
12 |
+
accept_multiple_files=True
|
13 |
+
)
|
14 |
+
|
15 |
+
if uploaded_images:
|
16 |
+
st.info(f"{len(uploaded_images)} images uploaded. Processing...")
|
17 |
+
|
18 |
+
# Open and convert to RGB
|
19 |
+
images = [Image.open(img).convert("RGB") for img in uploaded_images]
|
20 |
+
|
21 |
+
# Resize all to same width (use widest image)
|
22 |
+
max_width = max(img.width for img in images)
|
23 |
+
resized_images = []
|
24 |
+
|
25 |
+
for img in images:
|
26 |
+
if img.width != max_width:
|
27 |
+
ratio = max_width / img.width
|
28 |
+
new_height = int(img.height * ratio)
|
29 |
+
img = img.resize((max_width, new_height), Image.LANCZOS)
|
30 |
+
resized_images.append(img)
|
31 |
+
|
32 |
+
# Save all images as PDF (first image + append others)
|
33 |
+
pdf_buffer = io.BytesIO()
|
34 |
+
resized_images[0].save(
|
35 |
+
pdf_buffer,
|
36 |
+
format="PDF",
|
37 |
+
save_all=True,
|
38 |
+
append_images=resized_images[1:]
|
39 |
+
)
|
40 |
+
|
41 |
+
st.success("PDF created successfully!")
|
42 |
+
st.download_button(
|
43 |
+
label="⬇️ Download Merged PDF",
|
44 |
+
data=pdf_buffer.getvalue(),
|
45 |
+
file_name="merged_images.pdf",
|
46 |
+
mime="application/pdf"
|
47 |
+
)
|