|
import gradio as gr |
|
import fitz |
|
from PIL import Image |
|
import os |
|
|
|
def pdf_to_png(pdf_file, dpi=300): |
|
|
|
pdf_document = fitz.open(pdf_file) |
|
|
|
|
|
output_folder = "temp_output_images" |
|
if not os.path.exists(output_folder): |
|
os.makedirs(output_folder) |
|
|
|
|
|
png_files = [] |
|
|
|
|
|
for page_num in range(len(pdf_document)): |
|
|
|
page = pdf_document.load_page(page_num) |
|
|
|
|
|
pix = page.get_pixmap(matrix=fitz.Matrix(dpi/72, dpi/72)) |
|
|
|
|
|
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) |
|
|
|
|
|
output_path = os.path.join(output_folder, f"page_{page_num + 1}.png") |
|
img.save(output_path, "PNG") |
|
|
|
png_files.append(output_path) |
|
|
|
return png_files |
|
|
|
|
|
iface = gr.Interface( |
|
fn=pdf_to_png, |
|
inputs=[ |
|
gr.File(label="Upload PDF file"), |
|
gr.Slider(minimum=72, maximum=600, value=300, step=1, label="DPI") |
|
], |
|
outputs=[gr.Gallery(label="Converted PNG Images")], |
|
title="PDF to PNG Converter", |
|
description="Upload a PDF file and convert each page to a PNG image." |
|
) |
|
|
|
|
|
iface.launch() |