Skip to content

Commit 589b151

Browse files
Merge pull request geekcomputers#1237 from iamAmirrezaSaki/AmirrezaSaki
image2pdf converter
2 parents 7e7c5a0 + 0dcf9e2 commit 589b151

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# libraraies
2+
3+
import pytube
4+
import sys
5+
6+
7+
class YouTubeDownloder:
8+
def __init__(self):
9+
self.url = str(input("Enter the url of video : "))
10+
self.youtube = pytube.YouTube(
11+
self.url, on_progress_callback=YouTubeDownloder.onProgress)
12+
self.showTitle()
13+
14+
def showTitle(self):
15+
print("title : {0}\n".format(self.youtube.title))
16+
self.showStreams()
17+
18+
def showStreams(self):
19+
self.streamNo = 1
20+
for stream in self.youtube.streams:
21+
print("{0} => resolution:{1}/fps:{2}/type:{3}".format(self.streamNo,
22+
stream.resolution, stream.fps, stream.type))
23+
self.streamNo += 1
24+
self.chooseStream()
25+
26+
def chooseStream(self):
27+
self.choose = int(input("please select one : "))
28+
self.validateChooseValue()
29+
30+
def validateChooseValue(self):
31+
if self.choose in range(1, self.streamNo):
32+
self.getStream()
33+
else:
34+
print("please enter a correct option on the list.")
35+
self.chooseStream()
36+
37+
def getStream(self):
38+
self.stream = self.youtube.streams[self.choose-1]
39+
self.getFileSize()
40+
41+
def getFileSize(self):
42+
global file_size
43+
file_size = self.stream.filesize / 1000000
44+
self.getPermisionToContinue()
45+
46+
def getPermisionToContinue(self):
47+
print("\n title : {0} \n author : {1} \n size : {2:.2f}MB \n resolution : {3} \n fps : {4} \n ".format(
48+
self.youtube.title, self.youtube.author, file_size, self.stream.resolution, self.stream.fps))
49+
if input("do you want it ?(defualt = (y)es) or (n)o ") == "n":
50+
self.showStreams()
51+
else:
52+
self.main()
53+
54+
def download(self):
55+
self.stream.download()
56+
57+
@staticmethod
58+
def onProgress(stream=None, chunk=None, remaining=None):
59+
file_downloaded = (file_size-(remaining/1000000))
60+
print(
61+
f"downloading ... {file_downloaded/file_size*100:0.2f} % [{file_downloaded:.1f}MB of {file_size:.1f}MB]", end="\r")
62+
63+
def main(self):
64+
try:
65+
self.download()
66+
except KeyboardInterrupt:
67+
print("Canceled. ")
68+
sys.exit(0)
69+
70+
71+
if __name__ == "__main__":
72+
try:
73+
YouTubeDownloder()
74+
except KeyboardInterrupt:
75+
pass
76+
except Exception as e:
77+
print(e)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytube

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)