Spaces:
Runtime error
Runtime error
File size: 2,544 Bytes
e85dd68 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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() |