soiz1 commited on
Commit
63d758b
·
verified ·
1 Parent(s): faa9545

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import gradio as gr
2
  import subprocess
 
3
 
4
- def rasterize_pdf(input_pdf):
5
  output_pdf = "/tmp/output.pdf"
6
-
 
7
  cmd = [
8
  "gs",
9
  "-o", output_pdf,
@@ -16,19 +18,34 @@ def rasterize_pdf(input_pdf):
16
  "-dQUIET",
17
  input_pdf
18
  ]
19
-
20
  result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21
  if result.returncode != 0:
22
- return f"エラー: {result.stderr.decode()}", None
 
 
 
 
 
 
 
 
 
 
23
 
24
- return "ラスタライズ完了", output_pdf
25
 
26
  demo = gr.Interface(
27
- fn=rasterize_pdf,
28
  inputs=gr.File(file_types=[".pdf"]),
29
- outputs=[gr.Text(), gr.File()],
30
- title="PDFラスタライズ変換(Ghostscript)",
31
- description="SVGベクターPDFをビットマップ化PDFに変換し、Audiveris用に軽量化します。"
 
 
 
 
 
 
 
32
  )
33
 
34
  demo.launch()
 
1
  import gradio as gr
2
  import subprocess
3
+ import os
4
 
5
+ def rasterize_and_report(input_pdf):
6
  output_pdf = "/tmp/output.pdf"
7
+
8
+ # Ghostscriptコマンドで強制ラスタライズ
9
  cmd = [
10
  "gs",
11
  "-o", output_pdf,
 
18
  "-dQUIET",
19
  input_pdf
20
  ]
 
21
  result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
22
  if result.returncode != 0:
23
+ return f"Ghostscriptエラー: {result.stderr.decode()}", None, None
24
+
25
+ if not os.path.exists(output_pdf):
26
+ return "出力PDFが生成されませんでした", None, None
27
+
28
+ # 出力ファイルサイズ取得
29
+ size_bytes = os.path.getsize(output_pdf)
30
+ size_kb = size_bytes / 1024
31
+ size_str = f"{size_bytes} bytes ({size_kb:.1f} KB)"
32
+
33
+ return f"変換完了 / ファイルサイズ: {size_str}", output_pdf, size_str
34
 
 
35
 
36
  demo = gr.Interface(
37
+ fn=rasterize_and_report,
38
  inputs=gr.File(file_types=[".pdf"]),
39
+ outputs=[
40
+ gr.Text(label="結果メッセージ"),
41
+ gr.File(label="変換後PDF"),
42
+ gr.Text(label="変換後ファイルサイズ")
43
+ ],
44
+ title="PDFラスタライズ(Ghostscript)",
45
+ description=(
46
+ "SVGなどのベクターPDFを強制的に画像化(ラスタライズ)してPDF出力します。"
47
+ "Audiveris用にサイズを落とすのに便利です。"
48
+ )
49
  )
50
 
51
  demo.launch()