naohiro701 commited on
Commit
423e0a5
·
verified ·
1 Parent(s): b4baee7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import streamlit as st
2
- from wand.image import Image
3
- from wand.color import Color
4
  import io
5
  import zipfile
6
 
7
  # タイトルと説明
8
- st.title("PDFを高画質PNG画像に変換 (Wand)")
9
  st.write("""
10
  このアプリケーションでは、アップロードしたPDFファイルを高解像度(例:1000 dpi)のPNG画像に変換します。
11
  変換後、各ページの画像をダウンロード可能なZIPファイルとして提供します。
@@ -17,23 +17,22 @@ uploaded_file = st.file_uploader("PDFファイルをアップロードしてく
17
  if uploaded_file is not None:
18
  # DPIの設定
19
  dpi = st.slider("解像度(dpi)を選択してください", min_value=100, max_value=3000, value=1000, step=100)
20
-
21
  if st.button("変換開始"):
22
  try:
23
  with st.spinner("PDFを画像に変換中..."):
24
- with Image(blob=uploaded_file.read(), resolution=dpi) as img:
25
- img.compression_quality = 100
26
- img.alpha_channel = False
27
- images = img.sequence
28
-
29
- # ZIPファイルの作成
30
- zip_buffer = io.BytesIO()
31
- with zipfile.ZipFile(zip_buffer, "w") as zip_file:
32
- for i, page in enumerate(images):
33
- with Image(page) as page_image:
34
- png_blob = page_image.make_blob('png')
35
- zip_file.writestr(f"page_{i+1}.png", png_blob)
36
-
37
  zip_buffer.seek(0)
38
  st.success("変換が完了しました!")
39
  st.download_button(
@@ -45,10 +44,8 @@ if uploaded_file is not None:
45
 
46
  # 各ページの画像を表示(オプション)
47
  st.write("変換された画像:")
48
- for i, page in enumerate(images):
49
- with Image(page) as page_image:
50
- img = page_image.convert('png')
51
- st.image(img.make_blob('png'), caption=f"ページ {i+1}", use_column_width=True)
52
 
53
  except Exception as e:
54
  st.error(f"変換中にエラーが発生しました: {e}")
 
1
  import streamlit as st
2
+ from pdf2image import convert_from_bytes
3
+ from PIL import Image
4
  import io
5
  import zipfile
6
 
7
  # タイトルと説明
8
+ st.title("PDFを高画質PNG画像に変換")
9
  st.write("""
10
  このアプリケーションでは、アップロードしたPDFファイルを高解像度(例:1000 dpi)のPNG画像に変換します。
11
  変換後、各ページの画像をダウンロード可能なZIPファイルとして提供します。
 
17
  if uploaded_file is not None:
18
  # DPIの設定
19
  dpi = st.slider("解像度(dpi)を選択してください", min_value=100, max_value=3000, value=1000, step=100)
20
+
21
  if st.button("変換開始"):
22
  try:
23
  with st.spinner("PDFを画像に変換中..."):
24
+ # PDFを画像に変換
25
+ images = convert_from_bytes(uploaded_file.read(), dpi=dpi)
26
+
27
+ # ZIPファイルの作成
28
+ zip_buffer = io.BytesIO()
29
+ with zipfile.ZipFile(zip_buffer, "w") as zip_file:
30
+ for i, image in enumerate(images):
31
+ img_byte_arr = io.BytesIO()
32
+ image.save(img_byte_arr, format='PNG')
33
+ img_byte_arr.seek(0)
34
+ zip_file.writestr(f"page_{i+1}.png", img_byte_arr.read())
35
+
 
36
  zip_buffer.seek(0)
37
  st.success("変換が完了しました!")
38
  st.download_button(
 
44
 
45
  # 各ページの画像を表示(オプション)
46
  st.write("変換された画像:")
47
+ for i, image in enumerate(images):
48
+ st.image(image, caption=f"ページ {i+1}", use_column_width=True)
 
 
49
 
50
  except Exception as e:
51
  st.error(f"変換中にエラーが発生しました: {e}")