Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
from pdf2image import convert_from_path
|
4 |
-
|
5 |
-
import img2pdf
|
6 |
-
import os
|
7 |
-
import shutil
|
8 |
-
import PyPDF2
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
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 |
-
width_mm = width_pt * 25.4 / 72
|
19 |
-
height_mm = height_pt * 25.4 / 72
|
20 |
-
sizes.append((i, width_pt, height_pt, width_mm, height_mm))
|
21 |
-
return sizes
|
22 |
-
|
23 |
-
def downscale_pdf(pdf_file, dpi=150, max_width=1500):
|
24 |
with tempfile.TemporaryDirectory() as tmpdir:
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
for i, img in enumerate(images):
|
32 |
-
|
33 |
-
|
34 |
-
if w > max_width:
|
35 |
-
new_h = int(h * max_width / w)
|
36 |
-
img = img.resize((max_width, new_h), Image.LANCZOS)
|
37 |
-
resized_size = img.size
|
38 |
-
pixel_info.append((i, resized_size[0], resized_size[1]))
|
39 |
-
|
40 |
-
img_path = os.path.join(tmpdir, f"page_{i}.jpg")
|
41 |
-
img.save(img_path, "JPEG", quality=85)
|
42 |
-
downscaled_paths.append(img_path)
|
43 |
-
|
44 |
-
if not downscaled_paths:
|
45 |
-
raise ValueError("変換後の画像が見つかりません。")
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
result_file.close()
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Blocks() as demo:
|
72 |
-
gr.Markdown("#
|
|
|
|
|
73 |
with gr.Row():
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
pdf_output = gr.File(label="出力PDF")
|
82 |
-
size_output = gr.Textbox(label="出力PDFの詳細", lines=12)
|
83 |
-
|
84 |
-
convert_button.click(
|
85 |
-
fn=downscale_pdf,
|
86 |
-
inputs=[pdf_input, dpi_input, width_input],
|
87 |
-
outputs=[pdf_output, size_output]
|
88 |
-
)
|
89 |
|
90 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
from pdf2image import convert_from_path
|
4 |
+
import fitz # PyMuPDF
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def analyze_pdf(pdf_file):
|
7 |
+
results = []
|
8 |
+
overall_rating = "OK"
|
9 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
with tempfile.TemporaryDirectory() as tmpdir:
|
11 |
+
# pdf2imageで画像を取得
|
12 |
+
try:
|
13 |
+
images = convert_from_path(pdf_file.name, dpi=300, output_folder=tmpdir)
|
14 |
+
except Exception as e:
|
15 |
+
return f"PDF変換エラー: {str(e)}", ""
|
16 |
+
|
17 |
+
# 画像サイズ確認
|
18 |
for i, img in enumerate(images):
|
19 |
+
width, height = img.size
|
20 |
+
page_rating = "OK"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
if width < 1000 or height < 1000:
|
23 |
+
page_rating = "非推奨(解像度低)"
|
24 |
+
elif width < 1500 or height < 1500:
|
25 |
+
page_rating = "注意(やや低め)"
|
26 |
|
27 |
+
results.append(
|
28 |
+
f"ページ{i+1}: {width}x{height}px → {page_rating}"
|
29 |
+
)
|
|
|
30 |
|
31 |
+
if page_rating.startswith("非推奨"):
|
32 |
+
overall_rating = "非推奨"
|
33 |
+
elif page_rating.startswith("注意") and overall_rating == "OK":
|
34 |
+
overall_rating = "注意"
|
35 |
|
36 |
+
# 追加で、PDFメタ情報のDPIも参考に
|
37 |
+
doc = fitz.open(pdf_file.name)
|
38 |
+
dpi_infos = []
|
39 |
+
for i, page in enumerate(doc):
|
40 |
+
try:
|
41 |
+
# MediaBoxや解像度情報から概算
|
42 |
+
rect = page.rect
|
43 |
+
width_pt = rect.width
|
44 |
+
height_pt = rect.height
|
45 |
+
dpi_x = width / (width_pt / 72)
|
46 |
+
dpi_y = height / (height_pt / 72)
|
47 |
+
dpi_infos.append(f"ページ{i+1}推定DPI: {dpi_x:.1f}x{dpi_y:.1f}")
|
48 |
+
except Exception:
|
49 |
+
dpi_infos.append(f"ページ{i+1}推定DPI: 取得失敗")
|
50 |
+
|
51 |
+
# まとめ
|
52 |
+
result_text = "\n".join(results + [""] + dpi_infos)
|
53 |
+
result_text += f"\n\n総合評価: {overall_rating}"
|
54 |
+
|
55 |
+
return result_text, overall_rating
|
56 |
|
57 |
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("# Audiveris適性チェック(非公式・推定)")
|
59 |
+
gr.Markdown("PDFをアップロードすると、ページ画像サイズからAudiverisで使えそうかを推定します。")
|
60 |
+
|
61 |
with gr.Row():
|
62 |
+
pdf_input = gr.File(label="PDFファイル")
|
63 |
+
analyze_button = gr.Button("判定")
|
64 |
+
|
65 |
+
result_output = gr.Textbox(label="詳細結果", lines=20)
|
66 |
+
rating_output = gr.Textbox(label="総合評価")
|
67 |
+
|
68 |
+
analyze_button.click(analyze_pdf, inputs=pdf_input, outputs=[result_output, rating_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
if __name__ == "__main__":
|
71 |
+
demo.launch()
|