Skip to content

Commit

Permalink
Aspect Ratio Calculator is now properly usable
Browse files Browse the repository at this point in the history
FINALLY LET"S GOOOOOOOOOOOO
  • Loading branch information
Jangsoodlor committed Jun 24, 2022
1 parent 0ea3072 commit 93fbfec
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
43 changes: 27 additions & 16 deletions AspCalcTestModule.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
#-----------import stuffs--------------------#
from fpdf import FPDF
from PIL import Image
import os
from datetime import datetime
import tkinter as tk
from tkinter import filedialog

imagelist = []
w = 297
h = 210
w = 210
h = 297

folder = 'C:\\Users\\Jangsoodlor\\Desktop\\test\\landscapepik'

def AspCalc(w,h,im1):

if im1.width > im1.height:
pwratio = im1.width // w #594
phratio = im1.height // h #450
width = h
height = w
pwratio = im1.width // width #594
phratio = im1.height // height #450
if phratio >= pwratio:
new_width = im1.width // phratio
new_height = h
new_height = height
elif phratio < pwratio:
new_width = w
new_width = width
new_height = im1. height // pwratio
return new_width,new_height

elif im1.width <= im1.height:
width = w
height = h
pwratio = im1.width // width #594
phratio = im1.height // height #450
if phratio >= pwratio:
new_width = im1.width // phratio
new_height = height
elif phratio < pwratio:
new_width = width
new_height = im1. height // pwratio

return new_width,new_height



for item in os.listdir(folder):
Expand All @@ -31,15 +43,14 @@ def AspCalc(w,h,im1):
imagelist.append(full_path)
imagelist.sort() # Sort the images by name.


for i in range(0, len(imagelist)):
print(imagelist[i])


print('\nFound ' + str(len(imagelist)) + ' images. Converting to PDF....\n')
for i in range(0, len(imagelist)):
print(imagelist[i])
im1 = Image.open(imagelist[i]) # Open the image.
print(im1)
width, height = im1.size
print(AspCalc(w,h,im1))
print(AspCalc(w,h,im1)[1])


68 changes: 42 additions & 26 deletions JPG2PDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,43 @@ def openFolder():
file_path = filedialog.askdirectory()
return file_path

def AspCalc(w,h,im1):
a = im1.width // w
b = im1.height // a
if b <= h:
return int(b)
elif b > h:
c = im1.height // h
d = im1.width // c
return int(d)

# def AspCalc(w,h,im1):
# if im1.width > im1.height:
# pwratio = im1.width // w #594
# phratio = im1.height // h #450
# if phratio >= pwratio:
# new_width = im1.width // phratio
# new_height = h
# if phratio < pwratio:
# new_width = w
# new_height = im1. height // pwratio
# return new_width,new_height
# a = im1.width // w
# b = im1.height // a
# if b <= h:
# return int(b)
# elif b > h:
# c = im1.height // h
# d = im1.width // c
# return int(d)

def AspCalc(w,h,im1):

if im1.width > im1.height:
width = h
height = w
pwratio = im1.width // width #594
phratio = im1.height // height #450
if phratio >= pwratio:
new_width = im1.width // phratio
new_height = height
elif phratio < pwratio:
new_width = width
new_height = im1. height // pwratio

elif im1.width <= im1.height:
width = w
height = h
pwratio = im1.width // width #594
phratio = im1.height // height #450
if phratio >= pwratio:
new_width = im1.width // phratio
new_height = height
elif phratio < pwratio:
new_width = width
new_height = im1. height // pwratio

return new_width,new_height

while True:
# -------------- TUTORIAL ----#
Expand All @@ -54,7 +70,7 @@ def AspCalc(w,h,im1):
folder = rf'{folderinput}' # Folder containing all the images.
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'
aspect = str(input('keep your aspect ratio?[Y/N] (Default: Y): ')) or 'y'


# ---------------FPDF--------------------------------#
Expand All @@ -71,8 +87,6 @@ def AspCalc(w,h,im1):
imagelist.sort() # Sort the images by name.


for i in range(0, len(imagelist)):
print(imagelist[i])

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

Expand All @@ -93,23 +107,25 @@ def AspCalc(w,h,im1):
for i in range(0, len(imagelist)):
im1 = Image.open(imagelist[i]) # Open the image.
print(im1)
width, height = im1.size # Get the width and height of that image.
width, height = im1.size # Get the width and height of that image.
print(imagelist[i])
print(AspCalc(w,h,im1))

if width > height:
if aspect.lower() == 'n':
pdf.add_page('L')
pdf.image(imagelist[i], 0, 0, h, w)
elif aspect.lower() == 'y':
pdf.add_page('L')
pdf.image(imagelist[i], 0, 0, h, AspCalc(w,h,im1))
pdf.image(imagelist[i], 0, 0, AspCalc(w,h,im1)[0], AspCalc(w,h,im1)[1])

if width <= height:
if aspect.lower() == 'n':
pdf.add_page('P')
pdf.image(imagelist[i], 0, 0, w, h)
elif aspect.lower() == 'y':
pdf.add_page('P')
pdf.image(imagelist[i], 0, 0, w, AspCalc(w,h,im1))
pdf.image(imagelist[i], 0, 0, AspCalc(w,h,im1)[0], AspCalc(w,h,im1)[1])


pdf.output(folder + '\\' + name + '.pdf' , 'F') # Save the PDF.
Expand Down

0 comments on commit 93fbfec

Please sign in to comment.