-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_to_images.py
36 lines (28 loc) · 1.02 KB
/
pdf_to_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import fitz # PyMuPDF
from PIL import Image
import io
def pdf_to_images(pdf_path, output_folder):
"""
Convert each page of a PDF into an image and save to the output folder.
Parameters:
- pdf_path: Path to the PDF file.
- output_folder: Folder to save the resulting images.
"""
# Open the PDF
pdf_document = fitz.open(pdf_path)
for page_num in range(len(pdf_document)):
# Select the page
page = pdf_document.load_page(page_num)
# Render the page to a pixel-based image
pix = page.get_pixmap()
# Convert to PIL Image
img = Image.open(io.BytesIO(pix.tobytes()))
# Save image to file
image_path = f"{output_folder}/page_{page_num + 1}.png"
img.save(image_path)
print(f"Saved page {page_num + 1} as {image_path}")
pdf_document.close()
# Example usage
pdf_path = r'G:\Swipe\swipeinterntask\Sample invoices\invoice1.pdf'
output_folder = r'G:\Swipe\swipeinterntask\convertedfolder'
pdf_to_images(pdf_path, output_folder)