I'm using Image from PIL, to open images and load them into numpy arrays of dimension(300,300,3). These arrays are appended to a list, I then want to convert this list into a numpy array. Everything that should work, hasn't. I keep getting the weird error:
ValueError: could not broadcast input array from shape (300, 300, 3) into shape (300, 300)
Here is a small example code:
training_data = []
image = Image.open(path).resize((300,300),Image.ANTIALIAS)
training_data.append(np.asarray(image))
training_data = np.array(training_data)
the simple Issue is that I get the error talked about above. Any help is much appreciated.
Most likely you have collected a list of arrays of different sizes, perhaps some are b/w and others are color:
In [17]: alist = []
In [18]: alist.append(np.ones((300,300))) # bw
In [19]: alist.append(np.ones((300,300,3))) # color
In [20]: np.array(alist)
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
#!/usr/bin/python3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-7512d762195a> in <module>
----> 1 np.array(alist)
ValueError: could not broadcast input array from shape (300,300,3) into shape (300,300)
v1.19
gives us a warning when we try to make a array from arrays that differ in shape. Sometimes that still gives us an object dtype array, but with this combination of shapes, the result is your error.
===
An equivalent way of combining the arrays into one is with np.stack
. If it works the result is the same; if not, the error is different:
In [21]: np.stack(alist)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-724d9c1d0554> in <module>
----> 1 np.stack(alist)
<__array_function__ internals> in stack(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
425 shapes = {arr.shape for arr in arrays}
426 if len(shapes) != 1:
--> 427 raise ValueError('all input arrays must have the same shape')
428
429 result_ndim = arrays[0].ndim + 1
ValueError: all input arrays must have the same shape
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.