soiz1 commited on
Commit
a1622dd
·
verified ·
1 Parent(s): c45e793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -39
app.py CHANGED
@@ -1,53 +1,39 @@
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,
12
- "-sDEVICE=pdfwrite",
13
- "-dColorImageResolution=150",
14
- "-dGrayImageResolution=150",
15
- "-dMonoImageResolution=150",
16
- "-dFILTERVECTOR",
17
- "-dCompatibilityLevel=1.4",
18
- "-dNOPAUSE",
19
- "-dBATCH",
20
- "-dQUIET",
21
- input_pdf
22
- ]
23
- result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24
- if result.returncode != 0:
25
- return f"Ghostscriptエラー: {result.stderr.decode()}", None, None
26
 
27
- if not os.path.exists(output_pdf):
28
- return "出力PDFが生成されませんでした", None, None
29
 
30
- # 出力ファイルサイズ取得
31
- size_bytes = os.path.getsize(output_pdf)
32
- size_kb = size_bytes / 1024
33
- size_str = f"{size_bytes} bytes ({size_kb:.1f} KB)"
34
-
35
- return f"変換完了 / ファイルサイズ: {size_str}", output_pdf, size_str
36
 
 
37
 
38
  demo = gr.Interface(
39
- fn=rasterize_and_report,
40
  inputs=gr.File(file_types=[".pdf"]),
41
- outputs=[
42
- gr.Text(label="結果メッセージ"),
43
- gr.File(label="変換後PDF"),
44
- gr.Text(label="変換後ファイルサイズ")
45
- ],
46
- title="PDFラスタライズ(Ghostscript)",
47
- description=(
48
- "SVGなどのベクターPDFを強制的に画像化(ラスタライズ)してPDF出力します。"
49
- "Audiveris用にサイズを落とすのに便利です。"
50
- )
51
  )
52
 
53
  demo.launch()
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
+ import tempfile
5
 
6
+ def rasterize_with_imagemagick(input_pdf):
7
+ with tempfile.TemporaryDirectory() as tmpdir:
8
+ # PDF→PNG(72dpi)
9
+ png_pattern = os.path.join(tmpdir, "page_%03d.png")
10
+ cmd1 = ["convert", "-density", "72", input_pdf, png_pattern]
11
+ res1 = subprocess.run(cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
12
+ if res1.returncode != 0:
13
+ return f"ImageMagick変換エラー: {res1.stderr.decode()}", None, None
14
 
15
+ # PNG→PDFにまとめる
16
+ output_pdf = os.path.join(tmpdir, "output_rasterized.pdf")
17
+ cmd2 = ["convert", os.path.join(tmpdir, "page_*.png"), output_pdf]
18
+ res2 = subprocess.run(cmd2, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
19
+ if res2.returncode != 0:
20
+ return f"PDF作成エラー: {res2.stderr.decode()}", None, None
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ if not os.path.exists(output_pdf):
23
+ return "PDF生成に失敗しました", None, None
24
 
25
+ size_bytes = os.path.getsize(output_pdf)
26
+ size_kb = size_bytes / 1024
27
+ size_str = f"{size_bytes} bytes ({size_kb:.1f} KB)"
 
 
 
28
 
29
+ return "ImageMagickでラスタライズ完了", output_pdf, size_str
30
 
31
  demo = gr.Interface(
32
+ fn=rasterize_with_imagemagick,
33
  inputs=gr.File(file_types=[".pdf"]),
34
+ outputs=[gr.Text(), gr.File(), gr.Text()],
35
+ title="PDFをImageMagickでラスタライズ",
36
+ description="PDFを画像に変換して再PDF化し、ベクターを除去します。"
 
 
 
 
 
 
 
37
  )
38
 
39
  demo.launch()