app.py
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 |
import streamlit as st from pdf2image import convert_from_path from io import BytesIO from PIL import Image import tempfile # Title of the app st.title("Convert PDF to Images") # Instructions for the user st.write("Upload a PDF file, and this app will convert each page into a separate image.") # File uploader for PDF file uploaded_pdf = st.file_uploader("Choose a PDF file", type="pdf") # Process the PDF if a file is uploaded if uploaded_pdf: try: # Save the uploaded file temporarily with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf: temp_pdf.write(uploaded_pdf.read()) temp_pdf_path = temp_pdf.name # Convert the uploaded PDF to images pdf_pages = convert_from_path(temp_pdf_path, dpi=300) # Display and provide download button for each page for i, page in enumerate(pdf_pages): st.image(page, caption=f"Page {i + 1}", use_column_width=True) # Save the image to a BytesIO buffer img_buffer = BytesIO() page.save(img_buffer, format="JPEG") img_buffer.seek(0) # Provide download button for each page image st.download_button(label=f"Download Page {i + 1} as Image", data=img_buffer, file_name=f"page_{i + 1}.jpg", mime="image/jpeg") except Exception as e: st.error(f"An error occurred: {e}") |