Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
|
|
3 |
|
4 |
-
def
|
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=
|
28 |
inputs=gr.File(file_types=[".pdf"]),
|
29 |
-
outputs=[
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|