Skip to content

Commit

Permalink
Add Select Dialog and Tutorial
Browse files Browse the repository at this point in the history
:HappyNoises:
  • Loading branch information
Jangsoodlor committed Apr 15, 2022
1 parent a856a79 commit f35a50f
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions JPG2PDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,45 @@
from PIL import Image
import os
from datetime import datetime
import tkinter as tk
from tkinter import filedialog


#get time function#
def getDateTimeStr():
now = datetime.now()
return now.strftime('%Y%m%d_%H%M%S')

# open folder w/gui function#
def openFolder():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askdirectory()
return file_path

while True:
# -------------- TUTORIAL ----#

print("JPG-to-PDF with CLI v.3.2.0" + '\n' + 'Copyright (c) 2022 Jangsoodlor. All rights reserved.' + '\n')
print('Please move all of your pictures that you desired to be convert to a PDF file one folder before using this program.' + '\n'
+ 'If you left your field blank, the configuration of your PDF document will be automatically set by the default parameters.'+'\n')

# --------------- USER INPUT -------------------- #

folderinput = input("insert your picture's folder directory: ")
folderinput = input("insert your picture's folder directory (or Press ENTER to open select dialog): ") or openFolder()
folder = rf'{folderinput}' # Folder containing all the images.
name = str(input('insert your desired document name: ')) or getDateTimeStr() # Name of the output PDF file.
name = str(input('insert your desired document name (if left blank, your document will be named after the current time): ')) or getDateTimeStr() # Name of the output PDF file.
size = input('insert the paper size [A3/A4/A5] (Default: A4): ') or 'a4'
aspect = str(input('keep your aspect ratio?[Y/N] (Default: N): ')) or 'n'


# ---------------FPDF--------------------------------#
pdf = FPDF(format = f'{size}')
imagelist = [] # Contains the list of all images to be converted to PDF.


imagelist = [] # Contains the list of all images to be converted to PDF.


# ------------- ADD ALL THE IMAGES IN A LIST ------------- #
print('your directory is: ' + folder +'\n')

print('your directory is: ' + folder +'\n')
for item in os.listdir(folder):
if item.lower().endswith(('.png', '.jpg', '.jpeg')):
full_path = folder + '\\' + f'{item}'
Expand All @@ -40,20 +54,20 @@ def getDateTimeStr():

# --------------- DEFINE OUTPUT PAPER SIZE --------------------#

if size == 'A3' or size == 'a3':
if size.lower().endswith(('a3')):
w = int(297)
h = int (420)
elif size == 'A4' or size == 'a4':
elif size.lower().endswith(('a4')):
w = int(210)
h = int(297)
elif size == 'A5' or size == 'a5':
elif size.lower().endswith(('a5')):
w = int(148)
h = int (210)

# -------------- CONVERT TO PDF ------------ #


print('\nFound ' + str(len(imagelist)) + ' image files. Converting to PDF....\n')
print('\nFound ' + str(len(imagelist)) + ' images. Converting to PDF....\n')
for i in range(0, len(imagelist)):
im1 = Image.open(imagelist[i]) # Open the image.
print(im1)
Expand Down Expand Up @@ -89,9 +103,11 @@ def AspCalc():
pdf.output(folder + '\\' + name + '.pdf' , 'F') # Save the PDF.

print('PDF generated successfully!')


# ---------------Exit----#
print('Press Y if you want to exit, Otherwise press ENTER twice.:')
if input() == 'y' or input() == 'Y':
exit()

os.system('cls' if os.name == 'nt' else 'clear')
else:
os.system('cls' if os.name == 'nt' else 'clear')

0 comments on commit f35a50f

Please sign in to comment.