| 3 | |
| 4 | |
| 5 | class image2pdf: |
| 6 | def __init__(self): |
| 7 | self.validFormats = (".jpg", ".jpeg", ".png", ".JPG", ".PNG") |
| 8 | self.pictures = [] |
| 9 | |
| 10 | self.directory = "" |
| 11 | self.isMergePDF = True |
| 12 | |
| 13 | def getUserDir(self): |
| 14 | """Allow user to choose image directory""" |
| 15 | |
| 16 | msg = "\n1. Current directory\n2. Custom directory\nEnter a number: " |
| 17 | user_option = int(input(msg)) |
| 18 | |
| 19 | # Restrict input to either (1 or 2) |
| 20 | while user_option <= 0 or user_option >= 3: |
| 21 | user_option = int(input(f"\n*Invalid input*\n{msg}")) |
| 22 | |
| 23 | self.directory = ( |
| 24 | os.getcwd() if user_option == 1 else input("\nEnter custom directory: ") |
| 25 | ) |
| 26 | |
| 27 | def filter(self, item): |
| 28 | return item.endswith(self.validFormats) |
| 29 | |
| 30 | def sortFiles(self): |
| 31 | return sorted(os.listdir(self.directory)) |
| 32 | |
| 33 | def getPictures(self): |
| 34 | pictures = list(filter(self.filter, self.sortFiles())) |
| 35 | |
| 36 | if not pictures: |
| 37 | print(f" [Error] there are no pictures in the directory: {self.directory} ") |
| 38 | return False |
| 39 | |
| 40 | print("Found picture(s) :") |
| 41 | return pictures |
| 42 | |
| 43 | def selectPictures(self, pictures): |
| 44 | """Allow user to manually pick each picture or merge all""" |
| 45 | |
| 46 | listedPictures = {} |
| 47 | for index, pic in enumerate(pictures): |
| 48 | listedPictures[index + 1] = pic |
| 49 | print(f"{index + 1}: {pic}") |
| 50 | |
| 51 | userInput = ( |
| 52 | input( |
| 53 | "\n Enter the number(s) - (comma seperated/no spaces) or (A or a) to merge All \nChoice: " |
| 54 | ) |
| 55 | .strip() |
| 56 | .lower() |
| 57 | ) |
| 58 | |
| 59 | if userInput != "a": |
| 60 | # Convert user input (number) into corresponding (image title) |
| 61 | pictures = ( |
| 62 | listedPictures.get(int(number)) for number in userInput.split(",") |