Python Forum
What is the best way to return these 4 integer values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the best way to return these 4 integer values?
#1
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??

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)
Reply
#2
return (left, top, right, bottom) # one tuple
or
return((left, top), (right, bottom)) # tuple of tuples
I would define namedtuple in advance, e.g. Point with x and y or Rectangle with left, top, right and bottom. And will return namedtuple

from collections import namedtuple Rectangle = namedtuple('Rectangle', 'left, top, right, bottom') def get_int(prompt): while True: try: return int(input(prompt)) except TypeError: print('This is not a valid input. Try again.') 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)) crop_rect = Rectangle(left=200, top=200, right=1500, bottom=2000) # you can pass them as positional arguments, but kwargs make it more clear while True: im1 = picture.crop(crop_rect) # Fix the error, no need to unpack im1 = im1.resize(newsize) im1.show() answer = input('enter OK if you are happy, otherwise enter NO to try again: ') if answer.lower() == 'ok': break # break of the loop or you can directly do return crop_rect and remove the return statement from the end print('enter new values for top, left, bottom, right') crop_rect = Rectangle(*[get_int(f'Value for {coord}: ') for coord in Rectangle._fields]) return crop_rect
I also simplified your function and made another function to guarantee that input is int
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you very much, I've never used tuples before, wasn't sure how to handle them!

Slight hiccough:

I only changed getSize() by passing it jpgfiles[0]

def getSize(ajpg): print('Now getting the optimal size for the pdf ...') picture = Image.open(pathTojpgs + ajpg) width, height = picture.size print('jpg width = ' + str(width) + ' jpg height = ' + str(height)) crop_rect = Rectangle(left=200, top=200, right=1500, bottom=2000) # you can pass them as positional arguments, but kwargs make it more clear while True: im1 = picture.crop(*crop_rect) im1 = im1.resize(newsize) im1.show() answer = input('enter OK if you are happy, otherwise enter NO to try again: ') if answer.lower() == 'ok': break # break of the loop or you can directly do return crop_rect and remove the return statement from the end print('enter new values for top, left, bottom, right') crop_rect = Rectangle(*[get_int(f'Value for {coord}: ') for coord in Rectangle._fields]) return crop_rect
I tried to use getSize(ajpg) like this:

for file in pdfNames: print('splitting ' + file + ' to jpgs ') outputName = file.split('.') saveFilename = outputName[0] splitPDF(file, saveFilename) print('pdf split to jpgs!') print('now reize the jpgs ... ') jpgfiles = os.listdir(pathTojpgs) jpgfiles.sort() size1 = getSize(jpgfiles[0]) for jpg in jpgfiles: picture1 = Image.open(pathTojpgs + jpg) im1 = picture1.crop(size1) 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) junkjpgs(pathTojpgs) junkjpgs(pathToShrunkjpgs)
but getSize(ajpg) is showing this error:

Quote:>>> size1 = getSize(jpgfiles[0])
Now getting the optimal size for the pdf ...
jpg width = 1654 jpg height = 2339
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
size1 = getSize(jpgfiles[0])
File "<pyshell#60>", line 8, in getSize
im1 = picture.crop(*crop_rect)
TypeError: crop() takes from 1 to 2 positional arguments but 5 were given
>>>

I can't see the problem, can you please advise?
Reply
#4
sorry, I overlooked that - you don't need to unpack the tuple when you pass it to crop
 im1 = picture.crop(*crop_rect)
should be
 im1 = picture.crop(crop_rect)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks again!

I stopped yesterday, too long in front of the computer. It's nearly 6am now, I just tried it. Works great!

Quote:>>> size1
Rectangle(left=230, top=200, right=1330, bottom=1850)

That should keep the gf. happy!!

Obviously, your code is much more sophisticated than the rubbish I write. I'm still trying to understand it!

Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to return 2 values from 1 DF that equals another DF cubangt 5 2,473 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  [Solved]Return values from npyscreen Extra 2 2,685 Oct-09-2022, 07:19 PM
Last Post: Extra
  Parallelism with return values Plexian 7 3,647 Aug-14-2022, 09:33 AM
Last Post: Plexian
  please help with classes and return values jamie_01 5 3,308 Jan-17-2022, 02:11 AM
Last Post: menator01
  Need to parse a list of boolean columns inside a list and return true values Python84 4 4,140 Jan-09-2022, 02:39 AM
Last Post: Python84
  Function - Return multiple values tester_V 10 7,833 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 3,209 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  Pack integer values as single bytes in a struct bhdschmidt 3 4,480 Jun-09-2020, 09:23 PM
Last Post: bhdschmidt
  Return values for use outside of function willowman 1 2,825 Apr-13-2020, 07:00 AM
Last Post: buran
  Return all Values which can divided by 9 lastyle 2 3,066 Mar-16-2020, 09:22 PM
Last Post: lastyle

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.