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" )