-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine_pdfs.py
54 lines (42 loc) · 1.69 KB
/
combine_pdfs.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import sys
import math
import fitz # PyMuPDF
from PIL import Image
def convert_pdfs_to_image(pdf_folder, output_image_path, cols):
# Get a list of all PDF files in the folder
pdf_files = [file for file in os.listdir(pdf_folder) if file.endswith('.pdf')]
# Create a list to store images
images = []
# Convert each PDF to images
for pdf_file in pdf_files:
pdf_path = os.path.join(pdf_folder, pdf_file)
pdf_document = fitz.open(pdf_path)
# Create a list to store page images
page_images = []
# Convert each page to an image
for page_number in range(pdf_document.page_count):
page = pdf_document[page_number]
image = page.get_pixmap()
page_images.append(Image.frombytes("RGB", [image.width, image.height], image.samples))
# Close the PDF document
pdf_document.close()
# Append the page images to the main images list
images.extend(page_images)
# Determine the size of the final image
width, height = images[0].size
rows = math.ceil(len(images) / cols)
final_image = Image.new("RGB", (width * cols, height * rows), color="white")
# Paste each image into the final image
for i, image in enumerate(images):
row = i // cols
col = i % cols
final_image.paste(image, (col * width, row * height))
# Save the final image
final_image.save(output_image_path)
if __name__ == "__main__":
# Specify the input PDF folder and output image file
input_pdf_folder = sys.argv[1]
output_image_file = "maj3s_qiskit.png"
# Convert PDFs to image and save
convert_pdfs_to_image(input_pdf_folder, output_image_file, 4)