Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
from pdf2image import convert_from_path | |
from typing import List | |
def pdf_to_images( | |
pdf_file: str, | |
output_format: str = "jpg", | |
dpi: int = 200, | |
output_dir: str = "output_images" | |
) -> List[str]: | |
""" | |
将PDF转换为图片并保存到指定目录 | |
:param pdf_file: 上传的PDF文件路径 | |
:param output_format: 输出格式(jpg/png) | |
:param dpi: 分辨率(默认200) | |
:param output_dir: 输出目录名 | |
:return: 生成的图片路径列表 | |
""" | |
# 创建输出目录(如果不存在) | |
os.makedirs(output_dir, exist_ok=True) | |
# 转换PDF为图片 | |
images = convert_from_path(pdf_file, dpi=dpi) | |
# 保存图片 | |
saved_paths = [] | |
for i, image in enumerate(images): | |
output_path = os.path.join(output_dir, f"page_{i+1}.{output_format}") | |
image.save(output_path, "JPEG" if output_format == "jpg" else "PNG") | |
saved_paths.append(output_path) | |
return saved_paths | |
def process_pdf( | |
pdf_file: gr.File, | |
output_format: str, | |
dpi: int | |
) -> gr.Gallery: | |
""" | |
Gradio处理函数 | |
""" | |
if not pdf_file: | |
raise gr.Error("请先上传PDF文件!") | |
# 调用转换函数 | |
image_paths = pdf_to_images( | |
pdf_file.name, | |
output_format=output_format, | |
dpi=dpi | |
) | |
return image_paths | |
# Gradio界面 | |
with gr.Blocks(title="PDF转图片工具") as demo: | |
gr.Markdown("## 📄 PDF转图片工具") | |
gr.Markdown("上传PDF文件,选择输出格式和分辨率,点击“转换”按钮。") | |
with gr.Row(): | |
with gr.Column(): | |
pdf_input = gr.File(label="上传PDF文件", file_types=[".pdf"]) | |
output_format = gr.Radio( | |
choices=["jpg", "png"], | |
label="输出格式", | |
value="jpg" | |
) | |
dpi_slider = gr.Slider( | |
minimum=100, | |
maximum=600, | |
step=50, | |
value=200, | |
label="分辨率 (DPI)" | |
) | |
convert_btn = gr.Button("转换", variant="primary") | |
with gr.Column(): | |
gallery = gr.Gallery( | |
label="生成的图片", | |
columns=2, | |
height="auto" | |
) | |
# 绑定事件 | |
convert_btn.click( | |
fn=process_pdf, | |
inputs=[pdf_input, output_format, dpi_slider], | |
outputs=gallery | |
) | |
# 启动应用 | |
if __name__ == "__main__": | |
demo.launch() |