Skip to content
Advertisement

Iterating through a function returned 2-tuple containing two lists?

I am trying to learn the PIL’s image library and I am stuck yet a second time. What I am trying to do is read through a directory and all its subdirectories and only load, rotate and replace the images in a specific path entered by the user (or specified in the script). The problem is that line () in the code where I try to iterate through a function returned 2-tuple containing a list containing Imageobjects and another list containing strings raises the error:

“ValueError: too many values to unpack (expected 2)”.

I have tried doing a similar function in a separate script that returns the same type of 2-tuple that the get_subdirectory_images() function returns only without using any modules.

The real file with the error:

JavaScript

Which returns this when run:

ValueError: too many values to unpack (expected 2)

This is a small scale which I seemingly got to work as I wanted?

JavaScript

And it returns

JavaScript

The expected result is that all the images in the items subfolder should be rotated 90 degrees.

Advertisement

Answer

JavaScript

your function returns one tuple containing two lists of the same size.

Now the for loop iterates on the result, and first yields images which is a list of images (more than 2 images) that cannot fit in 2 variables. That explains the error message.

You could have built a dictionary or a list of tuples instead but if you want to iterate/associate images & paths you have to iterate on the zipped result

JavaScript

(variable names should be image, path, would be more logical)

As I mentionned above, returning a tuple of 2 lists, which must be associated together to be properly processed isn’t a good way of doing it.

Instead of doing:

JavaScript

just use images = [] at start, then:

JavaScript

then

JavaScript

now your unpacking works fine since each image/path tuple is associated when building the data structure.

Advertisement