soiz1 commited on
Commit
093fe97
·
verified ·
1 Parent(s): 0787dc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -76
app.py CHANGED
@@ -1,90 +1,71 @@
1
  import gradio as gr
2
  import tempfile
3
  from pdf2image import convert_from_path
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
- 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
- images = convert_from_path(pdf_file.name, dpi=dpi, output_folder=tmpdir)
26
- if not images:
27
- raise ValueError("PDFを画像に変換できませんでした。")
28
-
29
- downscaled_paths = []
30
- pixel_info = [] # 画像のピクセル情報を記録
 
31
  for i, img in enumerate(images):
32
- original_size = img.size
33
- w, h = original_size
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
- tmp_output_path = os.path.join(tmpdir, "downscaled.pdf")
48
- with open(tmp_output_path, "wb") as f_out:
49
- f_out.write(img2pdf.convert(downscaled_paths))
 
50
 
51
- result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
52
- with open(tmp_output_path, "rb") as src:
53
- shutil.copyfileobj(src, result_file)
54
- result_file.close()
55
 
56
- # サイズ情報(PDF)を取得
57
- pdf_sizes = get_pdf_page_sizes(result_file.name)
 
 
58
 
59
- # テキストとして合成
60
- info_lines = []
61
- for i, width_px, height_px in pixel_info:
62
- width_pt, height_pt, width_mm, height_mm = pdf_sizes[i][1:]
63
- info_lines.append(
64
- f"Page {i+1}:\n"
65
- f" - Pixel size : {width_px} x {height_px} px\n"
66
- f" - PDF size : {width_pt:.1f}pt x {height_pt:.1f}pt (~{width_mm:.1f}mm x {height_mm:.1f}mm)\n"
67
- )
68
-
69
- return result_file.name, "\n".join(info_lines)
 
 
 
 
 
 
 
 
 
70
 
71
  with gr.Blocks() as demo:
72
- gr.Markdown("# PDF 解像度ダウンサイザー\nPDFのページ画像を縮小して容量を減らします。")
 
 
73
  with gr.Row():
74
- with gr.Column():
75
- pdf_input = gr.File(label="入力PDF", file_types=[".pdf"])
76
- dpi_input = gr.Slider(72, 300, value=150, step=1, label="変換DPI")
77
- width_input = gr.Slider(500, 3000, value=1500, step=50, label="最大幅(px)")
78
- convert_button = gr.Button("変換")
79
-
80
- with gr.Column():
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
- demo.launch()
 
 
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()