Skip to content

Commit 0dcf9e2

Browse files
committed
add image2pdf converter to repo
1 parent 919b729 commit 0dcf9e2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

image2pdf/image2pdf.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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()

image2pdf/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pillow

0 commit comments

Comments
 (0)