I am writing a script to save a part of an image ("submatrix" in the code, reported below) as a tif file. When I run the code, I got the following error:
height, width = np.array(submatrix.shape, dtype = float) / dpi
ValueError: too many values to unpack
If instead of the submatrix I use a random matrix like np.random.random((10, 10)), everything runs fine. Do you spot what I am doing wrong?
Using
import matplotlib.pyplot as plt
import math
import numpy as np
Here's the code section where the error is:
submatrix = im[x_min:x_max, y_min:y_max]
dpi = size_box
height, width = np.array(submatrix.shape, dtype = float) / dpi
We get this error when there's a mismatch between the number of variables to the amount of values Python receives from a function, list, or other collection. The most straightforward way of avoiding this error is to consider how many values you need to unpack and then have the correct number of available variables.
The Python "ValueError: too many values to unpack (expected 3) in Python" occurs when the number of variables in the assignment is not the same as the number of values in the iterable. To solve the error, declare exactly as many variables as there are items in the iterable.
During a multiple value assignment, the ValueError: not enough values to unpack occurs when either you have fewer objects to assign than variables, or you have more variables than objects. This error caused by the mismatch between the number of values returned and the number of variables in the assignment statement.
The error message tells you what the problem is. There are too many values to unpack. Clearly submatrix.shape
has length greater than 2.
I cannot tell why that is so, since I don't know what im
is. But take a look at the following output from the interactive prompt:
>>> height, width = np.array([1,2], dtype = float) >>> height, width = np.array([1,2,3], dtype = float) Traceback (most recent call last): File "", line 1, in ValueError: too many values to unpack
Something like that is causing your problem with submatrix
fulfilling the role that [1,2,3]
does above.
If you strip away all the numpy, this is just a standard sequence unpacking error. The simplest example of that being:
>>> x, y = (1, 2, 3) Traceback (most recent call last): File "", line 1, in ValueError: too many values to unpack
When you perform sequence unpacking, the sequences on left and right hand side of the assignment operator must have the same length.
Any time I run into this, I set the output to a single var and the inspect that var to see what's inside.
>>> blah = np.array(submatrix.shape, dtype = float) / dpi
>>> dir(blah)
98% of the time, I simply miscounted the array size. (i.e. X, Y, and Z) the other 2% is usually due to variable length arrays (which really suck).
Early on, I would never use sequence unpack on the same line as the declaration, just to make debugging easier. I then set a break point on the height, width
line to see why it caused a problem.
blah = np.array(submatrix.shape, dtype = float) / dpi
height, width = blah
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With