Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from pdf2image import convert_from_path
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
def pdf_to_images(
|
| 7 |
+
pdf_file: str,
|
| 8 |
+
output_format: str = "jpg",
|
| 9 |
+
dpi: int = 200,
|
| 10 |
+
output_dir: str = "output_images"
|
| 11 |
+
) -> List[str]:
|
| 12 |
+
"""
|
| 13 |
+
将PDF转换为图片并保存到指定目录
|
| 14 |
+
:param pdf_file: 上传的PDF文件路径
|
| 15 |
+
:param output_format: 输出格式(jpg/png)
|
| 16 |
+
:param dpi: 分辨率(默认200)
|
| 17 |
+
:param output_dir: 输出目录名
|
| 18 |
+
:return: 生成的图片路径列表
|
| 19 |
+
"""
|
| 20 |
+
# 创建输出目录(如果不存在)
|
| 21 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
# 转换PDF为图片
|
| 24 |
+
images = convert_from_path(pdf_file, dpi=dpi)
|
| 25 |
+
|
| 26 |
+
# 保存图片
|
| 27 |
+
saved_paths = []
|
| 28 |
+
for i, image in enumerate(images):
|
| 29 |
+
output_path = os.path.join(output_dir, f"page_{i+1}.{output_format}")
|
| 30 |
+
image.save(output_path, "JPEG" if output_format == "jpg" else "PNG")
|
| 31 |
+
saved_paths.append(output_path)
|
| 32 |
+
|
| 33 |
+
return saved_paths
|
| 34 |
+
|
| 35 |
+
def process_pdf(
|
| 36 |
+
pdf_file: gr.File,
|
| 37 |
+
output_format: str,
|
| 38 |
+
dpi: int
|
| 39 |
+
) -> gr.Gallery:
|
| 40 |
+
"""
|
| 41 |
+
Gradio处理函数
|
| 42 |
+
"""
|
| 43 |
+
if not pdf_file:
|
| 44 |
+
raise gr.Error("请先上传PDF文件!")
|
| 45 |
+
|
| 46 |
+
# 调用转换函数
|
| 47 |
+
image_paths = pdf_to_images(
|
| 48 |
+
pdf_file.name,
|
| 49 |
+
output_format=output_format,
|
| 50 |
+
dpi=dpi
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return image_paths
|
| 54 |
+
|
| 55 |
+
# Gradio界面
|
| 56 |
+
with gr.Blocks(title="PDF转图片工具") as demo:
|
| 57 |
+
gr.Markdown("## 📄 PDF转图片工具")
|
| 58 |
+
gr.Markdown("上传PDF文件,选择输出格式和分辨率,点击“转换”按钮。")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
pdf_input = gr.File(label="上传PDF文件", file_types=[".pdf"])
|
| 63 |
+
output_format = gr.Radio(
|
| 64 |
+
choices=["jpg", "png"],
|
| 65 |
+
label="输出格式",
|
| 66 |
+
value="jpg"
|
| 67 |
+
)
|
| 68 |
+
dpi_slider = gr.Slider(
|
| 69 |
+
minimum=100,
|
| 70 |
+
maximum=600,
|
| 71 |
+
step=50,
|
| 72 |
+
value=200,
|
| 73 |
+
label="分辨率 (DPI)"
|
| 74 |
+
)
|
| 75 |
+
convert_btn = gr.Button("转换", variant="primary")
|
| 76 |
+
|
| 77 |
+
with gr.Column():
|
| 78 |
+
gallery = gr.Gallery(
|
| 79 |
+
label="生成的图片",
|
| 80 |
+
columns=2,
|
| 81 |
+
height="auto"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# 绑定事件
|
| 85 |
+
convert_btn.click(
|
| 86 |
+
fn=process_pdf,
|
| 87 |
+
inputs=[pdf_input, output_format, dpi_slider],
|
| 88 |
+
outputs=gallery
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# 启动应用
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|