-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage2pdf.py
42 lines (32 loc) · 1.22 KB
/
image2pdf.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
from PIL import Image
import os
class image2pdf:
def __init__(self):
self.validFormats = (".jpg", ".jpeg", ".png", ".JPG", ".PNG")
self.pictures = []
self.files = os.listdir()
self.convertPictures()
input("Done ..... (Press Any Key To Exit)")
def filter(self, item):
return item.endswith(self.validFormats)
def sortFiles(self):
return sorted(self.files)
def getPictures(self):
pictures = list(filter(self.filter, self.sortFiles()))
if self.isEmpty(pictures):
print(" [Error] there are no pictrues in the directory ! ")
raise Exception(" [Error] there are no pictrues in the directory !")
print("pictures are : \n {}".format(pictures))
return pictures
def isEmpty(self, items):
return True if len(items) == 0 else False
def convertPictures(self):
for picture in self.getPictures():
self.pictures.append(Image.open(picture).convert("RGB"))
self.save()
def save(self):
self.pictures[0].save(
"result.pdf", save_all=True, append_images=self.pictures[1:]
)
if __name__ == "__main__":
image2pdf()