Apr-13-2020, 08:49 AM
(This post was last modified: Apr-13-2020, 08:49 AM by Pedroski55.)
I have a little Python program to resize pdfs. Works OK for my purposes.
I want to cut it up into functions.
The first function takes the pdf and splits it into jpgs.
The next function opens 1 of the jpgs and should return the values for cropping the pdf. It should just return 4 integers, or 2 tuples (left,top) and (right, bottom)
I don't know how to return these values from this function. What is the best way to return them??
I want to cut it up into functions.
The first function takes the pdf and splits it into jpgs.
The next function opens 1 of the jpgs and should return the values for cropping the pdf. It should just return 4 integers, or 2 tuples (left,top) and (right, bottom)
I don't know how to return these values from this function. What is the best way to return them??
def getSize(): print('Now getting the optimal size for the pdf ...') jpgfiles = os.listdir(pathTojpgs) jpgfiles.sort() picture = Image.open(pathTojpgs + jpgfiles[0]) width, height = picture.size print('jpg width = ' + str(width) + ' jpg height = ' + str(height)) answer='NO' top = 200 left=200 bottom=2000 right=1500 im1 = picture.crop((left, top, right, bottom)) im1 = im1.resize(newsize) im1.show() answer = input('enter OK if you are happy, otherwise enter NO to try again ') while answer not in 'OK': print('enter new values for top, left, bottom, right') x1 = input('value for left ... ') y1 = input('value for top ... ') x2 = input('value for right ... ') y2 = input('value for bottom ... ') left = int(x1) top = int(y1) right = int(x2) bottom = int(y2) im1 = picture.crop((left, top, right, bottom)) im1 = im1.resize(newsize) im1.show() answer = input('enter OK if you are happy, otherwise enter NO to try again ') return HOW???? ;which will then go in this function:
for jpg in jpgfiles: picture1 = Image.open(pathTojpgs + jpg) im1 = picture1.crop((left, top, right, bottom)) im1 = im1.resize(newsize) output = jpg.split('.') savefilename = output[0] + 'shrunk.jpg' im1.save(pathToShrunkjpgs + savefilename) print(jpg + ' shrunk and saved to ' + pathToShrunkjpgs) name = output[0] jpg2pdf(name) # join the jpgs to a pdf again junkjpgs(pathTojpgs) # get rid of the mess junkjpgs(pathToShrunkjpgs)