Skip to content

Commit

Permalink
ADDED NEW FEATURES
Browse files Browse the repository at this point in the history
Added: option for user custom directory (getUserDir)
Added: option for user to manully pick the pictures (selectPictures) or merge them to one PDF
Added: option to rerun any option (change dir, convert or close)
Truncated: unused method (isEmpty) never been invoked
Improved: Implemented modern string formating using the f-string for readability
Improved: Provided the directory when showing the error (no pictures)
  • Loading branch information
OfficialAhmed committed Jul 24, 2023
1 parent 6f0af8b commit 5761e79
Showing 1 changed file with 109 additions and 18 deletions.
127 changes: 109 additions & 18 deletions image2pdf/image2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,128 @@ 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)")

self.directory = ""
self.isMergePDF = True


def getUserDir(self):
""" Allow user to choose image directory """

msg = "\n1. Current directory\n2. Custom directory\nEnter a number: "
user_option = int(input(msg))

# Restrict input to either (1 or 2)
while user_option <= 0 or user_option >= 3:
user_option = int(input(f"\n*Invalid input*\n{msg}"))

self.directory = os.getcwd() if user_option == 1 else input("\nEnter custom directory: ")

def filter(self, item):
return item.endswith(self.validFormats)

def sortFiles(self):
return sorted(self.files)
return sorted(os.listdir(self.directory))

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))

if not pictures:
print(f" [Error] there are no pictures in the directory: {self.directory} ")
return False

print(f"Found picture(s) :")
return pictures

def isEmpty(self, items):
return True if len(items) == 0 else False
def selectPictures(self, pictures):
""" Allow user to manually pick each picture or merge all """

listedPictures = {}
for index, pic in enumerate(pictures):
listedPictures[index+1] = pic
print(f"{index+1}: {pic}")

userInput = input("\n Enter the number(s) - (comma seperated/no spaces) or (A or a) to merge All \nChoice: ").strip().lower()

if userInput != "a":
# Convert user input (number) into corresponding (image title)
pictures = (
listedPictures.get(int(number)) for number in userInput.split(',')
)

self.isMergePDF = False

return pictures


def convertPictures(self):
for picture in self.getPictures():
self.pictures.append(Image.open(picture).convert("RGB"))
self.save()
"""
Convert pictures according the following:
* If pictures = 0 -> Skip
* If pictures = 1 -> use all
* Else -> allow user to pick pictures
def save(self):
self.pictures[0].save(
"result.pdf", save_all=True, append_images=self.pictures[1:]
)
Then determine to merge all or one pdf
"""

pictures = self.getPictures()
totalPictures = len(pictures) if pictures else 0

if totalPictures == 0:
return

elif totalPictures >= 2:
pictures = self.selectPictures(pictures)

if self.isMergePDF:
# All pics in one pdf.
for picture in pictures:
self.pictures.append(Image.open(f"{self.directory}\\{picture}").convert("RGB"))
self.save()

else:
# Each pic in seperate pdf.
for picture in pictures:
self.save(Image.open(f"{self.directory}\\{picture}").convert("RGB"), picture, False)

# Reset to default value for next run
self.isMergePDF = True
self.pictures = []
print(f"\n{'#'*30}")
print(" Done! ")
print(f"{'#'*30}\n")

def save(self, image=None, title="All-PDFs", isMergeAll=True):
# Save all to one pdf or each in seperate file

if isMergeAll:
self.pictures[0].save(
f"{self.directory}\\{title}.pdf",
save_all=True,
append_images=self.pictures[1:]
)

else:
image.save(f"{self.directory}\\{title}.pdf")


if __name__ == "__main__":
image2pdf()

# Get user directory only once
process = image2pdf()
process.getUserDir()
process.convertPictures()

# Allow user to rerun any process
while True:
user = input("Press (R or r) to Run again\nPress (C or c) to change directory\nPress (Any Key) To Exit\nchoice:").lower()
match user:
case "r":
process.convertPictures()
case "c":
process.getUserDir()
process.convertPictures()
case _:
break


0 comments on commit 5761e79

Please sign in to comment.