File size: 1,370 Bytes
e2a9023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

import streamlit as st
from PIL import Image
import io

st.set_page_config(page_title="Merge Images to PDF", layout="centered")
st.title("🖼️➡️📄 Merge Multiple Images into a PDF")

uploaded_images = st.file_uploader(
    "Upload multiple images (JPG, PNG, etc.)",
    type=["png", "jpg", "jpeg"],
    accept_multiple_files=True
)

if uploaded_images:
    st.info(f"{len(uploaded_images)} images uploaded. Processing...")

    # Open and convert to RGB
    images = [Image.open(img).convert("RGB") for img in uploaded_images]

    # Resize all to same width (use widest image)
    max_width = max(img.width for img in images)
    resized_images = []

    for img in images:
        if img.width != max_width:
            ratio = max_width / img.width
            new_height = int(img.height * ratio)
            img = img.resize((max_width, new_height), Image.LANCZOS)
        resized_images.append(img)

    # Save all images as PDF (first image + append others)
    pdf_buffer = io.BytesIO()
    resized_images[0].save(
        pdf_buffer,
        format="PDF",
        save_all=True,
        append_images=resized_images[1:]
    )

    st.success("PDF created successfully!")
    st.download_button(
        label="⬇️ Download Merged PDF",
        data=pdf_buffer.getvalue(),
        file_name="merged_images.pdf",
        mime="application/pdf"
    )