naohiro701 commited on
Commit
cb54174
·
verified ·
1 Parent(s): 0f9d1ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -24
app.py CHANGED
@@ -3,35 +3,33 @@ from pdf2image import convert_from_bytes
3
  from PIL import Image
4
  import io
5
  import zipfile
 
 
6
 
7
- def convert_pdf_to_images(pdf_byte_data, dpi_value):
8
  """
9
  Convert a PDF file in bytes to a list of PIL images using pdf2image.
10
- This function requires pdf2image and poppler installed on the system.
11
-
12
  Args:
13
  pdf_byte_data (bytes): PDF file in memory
14
  dpi_value (int): DPI setting for the conversion
15
-
16
  Returns:
17
  list: A list of PIL Image objects
18
  """
19
- images_list = convert_from_bytes(pdf_byte_data, dpi=dpi_value)
 
 
 
 
20
  return images_list
21
 
22
  def create_zip_from_images(images_list):
23
  """
24
  Create an in-memory ZIP file from a list of PIL images.
25
  Each image is saved as a PNG in the ZIP file.
26
-
27
- Args:
28
- images_list (list): A list of PIL Image objects
29
-
30
- Returns:
31
- BytesIO: A BytesIO object representing the in-memory ZIP file
32
  """
33
  zip_buf = io.BytesIO()
34
- with zipfile.ZipFile(zip_buf, 'w', zipfile.ZIP_DEFLATED) as zf:
35
  for idx, single_image in enumerate(images_list):
36
  img_buf = io.BytesIO()
37
  single_image.save(img_buf, format='PNG')
@@ -41,29 +39,43 @@ def create_zip_from_images(images_list):
41
  return zip_buf
42
 
43
  # Streamlit App
44
- st.title("PDFを高画質PNG画像に変換 (Hugging Face 版)")
45
  st.write(
46
  """
47
- このアプリケーションでは、アップロードしたPDFファイルを
48
- 指定したDPIでPNG画像に変換し、まとめてZIPファイルとしてダウンロードできます。
49
  """
50
  )
51
 
52
  uploaded_file = st.file_uploader("PDFファイルをアップロードしてください", type=["pdf"])
53
 
54
  if uploaded_file is not None:
55
- dpi = st.slider("解像度(dpi)を選択してください", min_value=100, max_value=3000, value=300, step=100)
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  if st.button("変換開始"):
58
  try:
59
  with st.spinner("PDFを画像に変換中..."):
60
- # Convert PDF -> list of images
61
- images_converted = convert_pdf_to_images(uploaded_file.read(), dpi)
 
 
 
 
62
 
63
- # Create zip in memory
64
  zip_file_bytes = create_zip_from_images(images_converted)
65
 
66
- # Download button
67
  st.success("変換が完了しました!")
68
  st.download_button(
69
  label="ZIPファイルをダウンロード",
@@ -72,10 +84,10 @@ if uploaded_file is not None:
72
  mime="application/zip"
73
  )
74
 
75
- # Show preview of each page
76
- st.write("変換された画像プレビュー:")
77
- for idx, single_image in enumerate(images_converted):
78
- st.image(single_image, caption=f"ページ {idx+1}", use_column_width=True)
79
 
80
  except Exception as e:
81
  st.error(f"変換中にエラーが発生しました: {e}")
 
3
  from PIL import Image
4
  import io
5
  import zipfile
6
+ import sys
7
+ import os
8
 
9
+ def convert_pdf_to_images(pdf_byte_data, dpi_value, poppler_path_value):
10
  """
11
  Convert a PDF file in bytes to a list of PIL images using pdf2image.
 
 
12
  Args:
13
  pdf_byte_data (bytes): PDF file in memory
14
  dpi_value (int): DPI setting for the conversion
15
+ poppler_path_value (str): poppler path (Windows user only)
16
  Returns:
17
  list: A list of PIL Image objects
18
  """
19
+ images_list = convert_from_bytes(
20
+ pdf_byte_data,
21
+ dpi=dpi_value,
22
+ poppler_path=poppler_path_value if poppler_path_value else None
23
+ )
24
  return images_list
25
 
26
  def create_zip_from_images(images_list):
27
  """
28
  Create an in-memory ZIP file from a list of PIL images.
29
  Each image is saved as a PNG in the ZIP file.
 
 
 
 
 
 
30
  """
31
  zip_buf = io.BytesIO()
32
+ with zipfile.ZipFile(zip_buf, "w", zipfile.ZIP_DEFLATED) as zf:
33
  for idx, single_image in enumerate(images_list):
34
  img_buf = io.BytesIO()
35
  single_image.save(img_buf, format='PNG')
 
39
  return zip_buf
40
 
41
  # Streamlit App
42
+ st.title("PDFを高画質PNG画像に変換")
43
  st.write(
44
  """
45
+ このアプリケーションでは、アップロードしたPDFファイルを高解像度(例:1000 dpi)のPNG画像に変換します。
46
+ 変換後、各ページの画像をダウンロード可能なZIPファイルとして提供します。
47
  """
48
  )
49
 
50
  uploaded_file = st.file_uploader("PDFファイルをアップロードしてください", type=["pdf"])
51
 
52
  if uploaded_file is not None:
53
+ # DPIの設定
54
+ dpi = st.slider("解像度(dpi)を選択してください", min_value=100, max_value=3000, value=1000, step=100)
55
+
56
+ # Windows向けのPopplerパス入力 (Mac/LinuxなどPOPPLERがPATHに通っている環境なら不要)
57
+ poppler_path_input = None
58
+ if sys.platform.startswith("win"):
59
+ poppler_path_input = st.text_input(
60
+ "Popplerのパスを入力してください(Windowsユーザーのみ)",
61
+ value="" # 例: "C:\\poppler-xx\\bin"
62
+ )
63
+ if poppler_path_input and not os.path.exists(poppler_path_input):
64
+ st.warning("指定されたPopplerのパスが存在しません。正しいパスを入力してください。")
65
 
66
  if st.button("変換開始"):
67
  try:
68
  with st.spinner("PDFを画像に変換中..."):
69
+ # PDF→画像へ変換
70
+ images_converted = convert_pdf_to_images(
71
+ uploaded_file.read(),
72
+ dpi_value=dpi,
73
+ poppler_path_value=poppler_path_input
74
+ )
75
 
76
+ # ZIPファイル作成
77
  zip_file_bytes = create_zip_from_images(images_converted)
78
 
 
79
  st.success("変換が完了しました!")
80
  st.download_button(
81
  label="ZIPファイルをダウンロード",
 
84
  mime="application/zip"
85
  )
86
 
87
+ # 各ページの画像を表示(オプション)
88
+ st.write("変換された画像:")
89
+ for i, image in enumerate(images_converted):
90
+ st.image(image, caption=f"ページ {i+1}", use_column_width=True)
91
 
92
  except Exception as e:
93
  st.error(f"変換中にエラーが発生しました: {e}")