Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,25 @@ from pdf2image import convert_from_path
|
|
4 |
from PIL import Image
|
5 |
import img2pdf
|
6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
def downscale_pdf(pdf_file, dpi=150, max_width=1500):
|
9 |
-
import shutil
|
10 |
|
|
|
11 |
with tempfile.TemporaryDirectory() as tmpdir:
|
12 |
# ステップ 1: PDF → 画像
|
13 |
images = convert_from_path(pdf_file.name, dpi=dpi, output_folder=tmpdir)
|
@@ -39,7 +54,11 @@ def downscale_pdf(pdf_file, dpi=150, max_width=1500):
|
|
39 |
shutil.copyfileobj(src, result_file)
|
40 |
result_file.close()
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
with gr.Blocks() as demo:
|
45 |
gr.Markdown("# PDF 解像度ダウンサイザー\nPDFのページ画像を縮小して容量を減らします。")
|
@@ -52,11 +71,12 @@ with gr.Blocks() as demo:
|
|
52 |
|
53 |
with gr.Column():
|
54 |
pdf_output = gr.File(label="出力PDF")
|
|
|
55 |
|
56 |
convert_button.click(
|
57 |
fn=downscale_pdf,
|
58 |
inputs=[pdf_input, dpi_input, width_input],
|
59 |
-
outputs=pdf_output
|
60 |
)
|
61 |
|
62 |
demo.launch()
|
|
|
4 |
from PIL import Image
|
5 |
import img2pdf
|
6 |
import os
|
7 |
+
import shutil
|
8 |
+
import PyPDF2
|
9 |
+
|
10 |
+
def get_pdf_page_sizes(pdf_path):
|
11 |
+
sizes = []
|
12 |
+
with open(pdf_path, "rb") as f:
|
13 |
+
reader = PyPDF2.PdfReader(f)
|
14 |
+
for i, page in enumerate(reader.pages):
|
15 |
+
mediabox = page.mediabox
|
16 |
+
width_pt = float(mediabox.width)
|
17 |
+
height_pt = float(mediabox.height)
|
18 |
+
# 1ポイント = 1/72インチ
|
19 |
+
width_mm = width_pt * 25.4 / 72
|
20 |
+
height_mm = height_pt * 25.4 / 72
|
21 |
+
sizes.append(f"Page {i+1}: {width_pt:.1f}pt x {height_pt:.1f}pt (~{width_mm:.1f}mm x {height_mm:.1f}mm)")
|
22 |
+
return "\n".join(sizes)
|
23 |
|
|
|
|
|
24 |
|
25 |
+
def downscale_pdf(pdf_file, dpi=150, max_width=1500):
|
26 |
with tempfile.TemporaryDirectory() as tmpdir:
|
27 |
# ステップ 1: PDF → 画像
|
28 |
images = convert_from_path(pdf_file.name, dpi=dpi, output_folder=tmpdir)
|
|
|
54 |
shutil.copyfileobj(src, result_file)
|
55 |
result_file.close()
|
56 |
|
57 |
+
# サイズ情報を取得
|
58 |
+
page_sizes_info = get_pdf_page_sizes(result_file.name)
|
59 |
+
|
60 |
+
return result_file.name, page_sizes_info
|
61 |
+
|
62 |
|
63 |
with gr.Blocks() as demo:
|
64 |
gr.Markdown("# PDF 解像度ダウンサイザー\nPDFのページ画像を縮小して容量を減らします。")
|
|
|
71 |
|
72 |
with gr.Column():
|
73 |
pdf_output = gr.File(label="出力PDF")
|
74 |
+
size_output = gr.Textbox(label="ページサイズ情報", lines=10)
|
75 |
|
76 |
convert_button.click(
|
77 |
fn=downscale_pdf,
|
78 |
inputs=[pdf_input, dpi_input, width_input],
|
79 |
+
outputs=[pdf_output, size_output]
|
80 |
)
|
81 |
|
82 |
demo.launch()
|