soiz1 commited on
Commit
0787dc4
·
verified ·
1 Parent(s): f8f9bae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -15,26 +15,27 @@ def get_pdf_page_sizes(pdf_path):
15
  mediabox = page.mediabox
16
  width_pt = float(mediabox.width)
17
  height_pt = float(mediabox.height)
18
- # 1ポイント = 1/72インチ
19
  width_mm = width_pt * 25.4 / 72
20
  height_mm = height_pt * 25.4 / 72
21
- sizes.append(f"Page {i+1}: {width_pt:.1f}pt x {height_pt:.1f}pt (~{width_mm:.1f}mm x {height_mm:.1f}mm)")
22
- return "\n".join(sizes)
23
-
24
 
25
  def downscale_pdf(pdf_file, dpi=150, max_width=1500):
26
  with tempfile.TemporaryDirectory() as tmpdir:
27
- # ステップ 1: PDF → 画像
28
  images = convert_from_path(pdf_file.name, dpi=dpi, output_folder=tmpdir)
29
  if not images:
30
- raise ValueError("PDFを画像に変換できませんでした。PDFが空か、Popplerが入っていないかもしれません。")
31
 
32
  downscaled_paths = []
 
33
  for i, img in enumerate(images):
34
- w, h = img.size
 
35
  if w > max_width:
36
  new_h = int(h * max_width / w)
37
  img = img.resize((max_width, new_h), Image.LANCZOS)
 
 
38
 
39
  img_path = os.path.join(tmpdir, f"page_{i}.jpg")
40
  img.save(img_path, "JPEG", quality=85)
@@ -43,22 +44,29 @@ def downscale_pdf(pdf_file, dpi=150, max_width=1500):
43
  if not downscaled_paths:
44
  raise ValueError("変換後の画像が見つかりません。")
45
 
46
- # ステップ 2: 画像 → PDF
47
  tmp_output_path = os.path.join(tmpdir, "downscaled.pdf")
48
  with open(tmp_output_path, "wb") as f_out:
49
  f_out.write(img2pdf.convert(downscaled_paths))
50
 
51
- # ここでtmpdirは消えるので、ファイルを残す
52
  result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
53
  with open(tmp_output_path, "rb") as src:
54
  shutil.copyfileobj(src, result_file)
55
  result_file.close()
56
 
57
- # サイズ情報を取得
58
- page_sizes_info = get_pdf_page_sizes(result_file.name)
59
 
60
- return result_file.name, page_sizes_info
 
 
 
 
 
 
 
 
61
 
 
62
 
63
  with gr.Blocks() as demo:
64
  gr.Markdown("# PDF 解像度ダウンサイザー\nPDFのページ画像を縮小して容量を減らします。")
@@ -71,7 +79,7 @@ with gr.Blocks() as demo:
71
 
72
  with gr.Column():
73
  pdf_output = gr.File(label="出力PDF")
74
- size_output = gr.Textbox(label="ページサイズ情報", lines=10)
75
 
76
  convert_button.click(
77
  fn=downscale_pdf,
 
15
  mediabox = page.mediabox
16
  width_pt = float(mediabox.width)
17
  height_pt = float(mediabox.height)
 
18
  width_mm = width_pt * 25.4 / 72
19
  height_mm = height_pt * 25.4 / 72
20
+ sizes.append((i, width_pt, height_pt, width_mm, height_mm))
21
+ return sizes
 
22
 
23
  def downscale_pdf(pdf_file, dpi=150, max_width=1500):
24
  with tempfile.TemporaryDirectory() as tmpdir:
 
25
  images = convert_from_path(pdf_file.name, dpi=dpi, output_folder=tmpdir)
26
  if not images:
27
+ raise ValueError("PDFを画像に変換できませんでした。")
28
 
29
  downscaled_paths = []
30
+ pixel_info = [] # 画像のピクセル情報を記録
31
  for i, img in enumerate(images):
32
+ original_size = img.size
33
+ w, h = original_size
34
  if w > max_width:
35
  new_h = int(h * max_width / w)
36
  img = img.resize((max_width, new_h), Image.LANCZOS)
37
+ resized_size = img.size
38
+ pixel_info.append((i, resized_size[0], resized_size[1]))
39
 
40
  img_path = os.path.join(tmpdir, f"page_{i}.jpg")
41
  img.save(img_path, "JPEG", quality=85)
 
44
  if not downscaled_paths:
45
  raise ValueError("変換後の画像が見つかりません。")
46
 
 
47
  tmp_output_path = os.path.join(tmpdir, "downscaled.pdf")
48
  with open(tmp_output_path, "wb") as f_out:
49
  f_out.write(img2pdf.convert(downscaled_paths))
50
 
 
51
  result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
52
  with open(tmp_output_path, "rb") as src:
53
  shutil.copyfileobj(src, result_file)
54
  result_file.close()
55
 
56
+ # サイズ情報(PDF)を取得
57
+ pdf_sizes = get_pdf_page_sizes(result_file.name)
58
 
59
+ # テキストとして合成
60
+ info_lines = []
61
+ for i, width_px, height_px in pixel_info:
62
+ width_pt, height_pt, width_mm, height_mm = pdf_sizes[i][1:]
63
+ info_lines.append(
64
+ f"Page {i+1}:\n"
65
+ f" - Pixel size : {width_px} x {height_px} px\n"
66
+ f" - PDF size : {width_pt:.1f}pt x {height_pt:.1f}pt (~{width_mm:.1f}mm x {height_mm:.1f}mm)\n"
67
+ )
68
 
69
+ return result_file.name, "\n".join(info_lines)
70
 
71
  with gr.Blocks() as demo:
72
  gr.Markdown("# PDF 解像度ダウンサイザー\nPDFのページ画像を縮小して容量を減らします。")
 
79
 
80
  with gr.Column():
81
  pdf_output = gr.File(label="出力PDF")
82
+ size_output = gr.Textbox(label="出力PDFの詳細", lines=12)
83
 
84
  convert_button.click(
85
  fn=downscale_pdf,