File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from PIL import Image
2+ import os
3+
4+ class image2pdf :
5+ def __init__ (self ):
6+ self .validFormats = (
7+ '.jpg' ,
8+ '.jpeg' ,
9+ '.png' ,
10+ '.JPG' ,
11+ '.PNG'
12+ )
13+ self .pictures = []
14+ self .files = os .listdir ()
15+ self .convertPictures ()
16+ input ('Done ..... (Press Any Key To Exit)' )
17+
18+
19+ def filter (self , item ):
20+ return item .endswith (self .validFormats )
21+
22+
23+ def sortFiles (self ):
24+ return sorted (self .files )
25+
26+
27+ def getPictures (self ):
28+ pictures = list (filter (self .filter , self .sortFiles ()))
29+ if self .isEmpty (pictures ):
30+ print (" [Error] there are no pictrues in the directory ! " )
31+ raise Exception (" [Error] there are no pictrues in the directory !" )
32+ print ('pictures are : \n {}' .format (pictures ))
33+ return pictures
34+
35+ def isEmpty (self , items ):
36+ return True if len (items ) == 0 else False
37+
38+ def convertPictures (self ):
39+ for picture in self .getPictures ():
40+ self .pictures .append (Image .open (picture ).convert ('RGB' ))
41+ self .save ()
42+
43+
44+ def save (self ):
45+ self .pictures [0 ].save ('result.pdf' , save_all = True , append_images = self .pictures [1 :])
46+
47+
48+ if __name__ == "__main__" :
49+ image2pdf ()
Original file line number Diff line number Diff line change 1+ pillow
You can’t perform that action at this time.
0 commit comments