soiz1 commited on
Commit
c8dac6a
·
verified ·
1 Parent(s): 03f50bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -56
app.py CHANGED
@@ -5,65 +5,41 @@ import img2pdf
5
  import tempfile
6
  import os
7
 
8
- def downscale_pdf(pdf_file, max_width=3000, input_dpi=150):
 
 
 
9
  with tempfile.TemporaryDirectory() as tmpdir:
10
- # 1. dpiを下げて読み込み(元が300なら150に半減)
11
- images = convert_from_path(pdf_file.name, dpi=input_dpi, fmt='jpeg', output_folder=tmpdir)
12
-
13
- downscaled_images = []
14
  for i, img in enumerate(images):
15
- w, h = img.size
16
- print(f"Original Page {i+1}: {w}x{h} = {w * h} px")
17
-
18
- # サイズ制限
19
- if w > max_width:
20
- ratio = max_width / w
21
- new_w = int(w * ratio)
22
- new_h = int(h * ratio)
23
- img = img.resize((new_w, new_h), Image.LANCZOS)
24
- print(f"Downscaled Page {i+1}: {new_w}x{new_h} = {new_w * new_h} px")
25
- else:
26
- new_w, new_h = w, h
27
- print(f"Page {i+1}: サイズ変更なし")
28
-
29
- tmp_image_path = os.path.join(tmpdir, f"page_{i+1}.jpg")
30
- img.save(tmp_image_path, "JPEG", quality=90)
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("## PDFダウンサイザー for Audiveris")
59
- gr.Markdown(
60
- "PDFをアップロードすると、ページ画像を縮小してAudiveris向けに最適化したPDFを生成します。"
61
- )
62
- with gr.Row():
63
- pdf_input = gr.File(label="PDFファイルをアップロード")
64
- pdf_output = gr.File(label="変換後PDF")
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()