-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg2pdf.py
65 lines (55 loc) · 2.15 KB
/
img2pdf.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
55
56
57
58
59
60
61
62
63
64
65
"""This script is to combine image files into a PDF because it is a pain in the
ass for me to have to do it online and then move the file from my downloads
and rename it every time I want to submit an assignment."""
import tkinter as tk
import tkinter.filedialog
from PIL import Image
from fpdf import FPDF
def rgba2rgb(rgba: Image) -> Image:
"""This function converts images with an alpha channel (RGBA) to images
without (RGB). RGBA images cannot be saved as PDF."""
rgb = Image.new('RGB', rgba.size, (255, 255, 255)) # white background
if(len(rgba.split())>=4):
rgb.paste(rgba, mask=rgba.split()[3])
#Remove the alpha channel if there is one.
else:
rgb = rgba
#If there is no alpha channel, then just return the input image.
return rgb
def img2pdf(image_list: list, destination: str) -> None:
"""This function converts a list of images into a PDF.
Arguments:
image_list -- a list object containing string file paths to the images
destination -- a string object containing the destination file path of the
output PDF
"""
image_objects = [rgba2rgb(Image.open(image)) for image in image_list]
#open all the images from their file paths and store them into this list
#also, convert them from RGBA to RGB
if len(image_objects) == 1:
image_objects[0].save(
destination, "PDF",
resolution=100.0,
save_all=True
)
elif len(image_objects)>1:
image_objects[0].save(
destination, "PDF",
resolution=100.0,
save_all=True,
append_images=image_objects[1:]
)
else:
print("Select at least one image!")
def main():
"""Default behaviour if this module is run as a script"""
images = tk.filedialog.askopenfilenames()
#Graphically select input image files
destination_path = tk.filedialog.asksaveasfilename(
defaultextension="pdf",
filetypes=[("PDF File", "*.pdf")]
)
#Graphically select output destination & file name
img2pdf(images,destination_path)
if __name__ == "__main__":
main()#change argument for each module