Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image | |
from fpdf import FPDF | |
import io | |
import os | |
# Streamlit app | |
st.title("SlimShadow 🤗 Image to PDF Converter") | |
uploaded_files = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True) | |
page_numbering = st.checkbox("Enable Page Numbering") | |
if uploaded_files: | |
images = [] | |
for file in uploaded_files: | |
image = Image.open(file) | |
images.append(image) | |
if st.button("Generate PDF"): | |
pdf = FPDF() | |
for i, image in enumerate(images): | |
# Save the image to a byte stream | |
image_io = io.BytesIO() | |
image.save(image_io, format="JPEG") | |
image_io.seek(0) | |
# Save the byte stream to a temporary file | |
temp_filename = f'temp_image_{i}.jpg' | |
with open(temp_filename, 'wb') as temp_file: | |
temp_file.write(image_io.read()) | |
# Add a new page to the PDF and insert the image | |
pdf.add_page() | |
pdf.image(temp_filename, x=10, y=10, w=pdf.w - 20) | |
# Add page number if enabled | |
if page_numbering: | |
pdf.set_y(-25) | |
pdf.set_font("Arial", size=12) | |
pdf.cell(0, 10, f'Page {i+1}', 0, 0, 'C') | |
# Remove temporary file | |
os.remove(temp_filename) | |
# Get the PDF content as bytes | |
pdf_output = io.BytesIO(pdf.output(dest='S').encode('latin1')) | |
st.download_button("Download PDF", pdf_output, "images.pdf", "application/pdf") | |
st.success("PDF Generated successfully!") | |