Skip to content

Instantly share code, notes, and snippets.

@kapiecii
Created July 13, 2024 09:26
Show Gist options
  • Save kapiecii/a42f28dcdfea1854ec7efcaa0544cd27 to your computer and use it in GitHub Desktop.
Save kapiecii/a42f28dcdfea1854ec7efcaa0544cd27 to your computer and use it in GitHub Desktop.
import os
from pdf2image import convert_from_path
from PIL import Image
def pdf_to_single_image(pdf_path, output_path, dpi=150):
pages = convert_from_path(pdf_path, dpi=dpi)
# 最初のページのサイズを取得
width, height = pages[0].size
# 全ページの高さを合計
total_height = sum(page.height for page in pages)
# 新しい画像を作成(幅は同じ、高さは全ページの合計)
combined_image = Image.new('RGB', (width, total_height))
# 各ページを新しい画像に貼り付け
y_offset = 0
for page in pages:
combined_image.paste(page, (0, y_offset))
y_offset += page.height
# 結合した画像を保存
combined_image.save(output_path)
print(f"Saved combined image to {output_path}")
pdf_path = "./data/file-name.pdf"
output_path = "./data/file-name.png"
pdf_to_single_image(pdf_path, output_path)
@kapiecii
Copy link
Author

Install

pip install pdf2image Pillow
apt update
apt install -y poppler-utils

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment