Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
import os
|
|
|
4 |
|
5 |
-
def
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
"
|
11 |
-
|
12 |
-
|
13 |
-
|
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 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
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=
|
40 |
inputs=gr.File(file_types=[".pdf"]),
|
41 |
-
outputs=[
|
42 |
-
|
43 |
-
|
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()
|