Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,65 +5,41 @@ import img2pdf
|
|
5 |
import tempfile
|
6 |
import os
|
7 |
|
8 |
-
def
|
|
|
|
|
|
|
9 |
with tempfile.TemporaryDirectory() as tmpdir:
|
10 |
-
#
|
11 |
-
images = convert_from_path(pdf_file.name, dpi=
|
12 |
-
|
13 |
-
|
14 |
for i, img in enumerate(images):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
downscaled_images.append((tmp_image_path, (new_w, new_h)))
|
32 |
-
|
33 |
-
# 2. img2pdfのdpiを画像サイズから計算する
|
34 |
-
# dpi = (元画像のdpi) * (縮小率)
|
35 |
-
# 今回は画像のピクセル数が縮小されているので、それに対応するdpiを設定する
|
36 |
-
# ここでは単純に input_dpi をそのまま使うことも可
|
37 |
-
|
38 |
-
pdf_bytes = b""
|
39 |
-
layout_fun = img2pdf.get_layout_fun(pagesize=None) # デフォルトページサイズ
|
40 |
-
|
41 |
-
with open(os.path.join(tmpdir, "downscaled.pdf"), "wb") as f:
|
42 |
-
f.write(
|
43 |
-
img2pdf.convert(
|
44 |
-
[path for path, size in downscaled_images],
|
45 |
-
dpi=input_dpi,
|
46 |
-
)
|
47 |
-
)
|
48 |
-
|
49 |
-
final_pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
50 |
-
with open(os.path.join(tmpdir, "downscaled.pdf"), "rb") as src, open(final_pdf_file.name, "wb") as dst:
|
51 |
-
dst.write(src.read())
|
52 |
-
|
53 |
-
return final_pdf_file.name
|
54 |
-
|
55 |
-
|
56 |
|
57 |
with gr.Blocks() as demo:
|
58 |
-
gr.Markdown("
|
59 |
-
gr.
|
60 |
-
|
61 |
-
)
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
convert_button = gr.Button("変換してダウンロード")
|
67 |
-
convert_button.click(fn=downscale_pdf, inputs=pdf_input, outputs=pdf_output)
|
68 |
|
69 |
demo.launch()
|
|
|
5 |
import tempfile
|
6 |
import os
|
7 |
|
8 |
+
def resize_pdf(pdf_file, width_percent, height_percent):
|
9 |
+
width_percent = float(width_percent)
|
10 |
+
height_percent = float(height_percent)
|
11 |
+
|
12 |
with tempfile.TemporaryDirectory() as tmpdir:
|
13 |
+
# PDF → 画像(全ページ)
|
14 |
+
images = convert_from_path(pdf_file.name, dpi=200, output_folder=tmpdir)
|
15 |
+
|
16 |
+
resized_images = []
|
17 |
for i, img in enumerate(images):
|
18 |
+
# リサイズ幅・高さ計算
|
19 |
+
new_width = int(img.width * width_percent / 100)
|
20 |
+
new_height = int(img.height * height_percent / 100)
|
21 |
+
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
|
22 |
+
|
23 |
+
# 一時保存
|
24 |
+
img_path = os.path.join(tmpdir, f"page_{i}.png")
|
25 |
+
resized_img.save(img_path, format='PNG')
|
26 |
+
resized_images.append(img_path)
|
27 |
+
|
28 |
+
# 画像 → PDF
|
29 |
+
output_pdf_path = os.path.join(tmpdir, "resized_output.pdf")
|
30 |
+
with open(output_pdf_path, "wb") as f_out:
|
31 |
+
f_out.write(img2pdf.convert(resized_images))
|
32 |
+
|
33 |
+
return output_pdf_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# PDFサイズを%でリサイズ")
|
37 |
+
pdf_input = gr.File(label="PDFファイルを選択")
|
38 |
+
width_input = gr.Number(label="幅の倍率(%)", value=100)
|
39 |
+
height_input = gr.Number(label="高さの倍率(%)", value=100)
|
40 |
+
output_pdf = gr.File(label="リサイズ後のPDF")
|
41 |
+
|
42 |
+
btn = gr.Button("リサイズ実行")
|
43 |
+
btn.click(resize_pdf, inputs=[pdf_input, width_input, height_input], outputs=output_pdf)
|
|
|
|
|
44 |
|
45 |
demo.launch()
|