-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdf_finish.py
38 lines (31 loc) · 1.24 KB
/
pdf_finish.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
import sys
from PIL import Image
def save_images_as_pdf(images, pdf_filename, resolution=100.0):
rgb_list = []
for image in images:
if image.mode is 'RGB':
rgb_list.append(image)
else:
rgb = Image.new('RGB', image.size, (255, 255, 255))
rgb.paste(image, mask=image.split()[3])
rgb_list.append(rgb)
rgb_list[0].save(pdf_filename, "PDF", resolution=resolution, save_all=True, append_images=rgb_list[1:])
if __name__ == "__main__":
if (len(sys.argv) == 1 or sys.argv[1] == "--help"):
print("Usage: pdf_finish.py [pdf_filename] [image_filename]+")
print("Example: pdf_finish.py cards.pdf cards_0.png cards_1.png")
print(" this means: \"create a pdf file named 'cards.pdf' with the pages 'cards_0.png' and 'cards_1.png'\"")
exit(1)
try:
pdf_filename = str(sys.argv[1])
image_names = []
for i in range(2, len(sys.argv)):
image_names.append(sys.argv[i])
except:
print("Type \"pdf_finish.py --help\" to see usage.")
exit(1)
im_list = []
for image_name in image_names:
im = Image.open(image_name)
im_list.append(im)
save_images_as_pdf(im_list, pdf_filename)