File size: 950 Bytes
0df76e5
 
aa2ee61
0df76e5
aa2ee61
 
 
 
 
 
 
 
 
3539a90
00fd5b4
aa2ee61
0df76e5
aa2ee61
 
 
 
00fd5b4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import gradio as gr
from pdf2image import convert_from_path
from io import BytesIO

def pdf_to_images(pdf_file):
    images = convert_from_path(pdf_file.name)  # Use the name to read the file
    img_list = []
    for i, image in enumerate(images):
        img_buffer = BytesIO()
        image.save(img_buffer, format="PNG")  # Save image as PNG
        img_buffer.seek(0)  # Go to the start of the BytesIO buffer
        img_list.append(img_buffer.getvalue())  # Append the byte data of the image
    return img_list

with gr.Blocks() as demo:
    gr.Markdown("# PDF to Image Converter")

    pdf_input = gr.File(label="Upload PDF", type="file")  # Correctly specify 'file'
    output_gallery = gr.Gallery(label="Converted Images", scale=1.0, show_label=True, elem_id="gallery")  # Removed the .style() method

    pdf_input.change(fn=pdf_to_images, inputs=pdf_input, outputs=output_gallery)  # Update output when the PDF is uploaded

demo.launch()