0

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:

  • before loop
training_data = []
  • in loop
image = Image.open(path).resize((300,300),Image.ANTIALIAS)
training_data.append(np.asarray(image))
  • outside loop
training_data = np.array(training_data)

the simple Issue is that I get the error talked about above. Any help is much appreciated.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Reading your answer, I decided to pull all the shape data into a file to look over the shapes, and I have found 100% consistent shapes between all elements of the list.
You could try np.stack(training_data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.