In [189]: color_list = np.array([[1,2,3],[4,5,6]])
In [190]: color_list.shape
Out[190]: (2, 3)
In [191]: image = np.ones((4,5,3),int)
In [192]: all_t = np.array([])
...: for color in color_list:
...: t = np.subtract(image, color)
...: all_t = np.append(all_t, t)
...:
In [193]: all_t.shape
Out[193]: (120,)
all_t is a 1d array. np.append is clear that without axis it flattens/ravels all inputs.
Your reshape is wrong, ignoring the size 3 dimension:
In [194]: all_t.reshape(len(color_list), image.shape[0], image.shape[1])
Traceback (most recent call last):
File "<ipython-input-194-c246f37a52d0>", line 1, in <module>
all_t.reshape(len(color_list), image.shape[0], image.shape[1])
ValueError: cannot reshape array of size 120 into shape (2,4,5)
A working reshape:
In [195]: x=all_t.reshape(len(color_list), image.shape[0], image.shape[1],3)
In [196]: x.shape
Out[196]: (2, 4, 5, 3)
If you are going to build an an array iteratively, use list append. It is faster, and easier to use correctly:
In [197]: all_t = []
...: for color in color_list:
...: t = np.subtract(image, color)
...: all_t.append(t)
...:
In [198]: len(all_t)
Out[198]: 2
In [199]: y = np.array(all_t)
In [200]: y.shape
Out[200]: (2, 4, 5, 3)
In [201]: np.allclose(x,y)
Out[201]: True
But there's no need to iterate on the first dimension of color_list:
In [202]: z = image - color_list[:,None,None,:]
In [203]: z.shape
Out[203]: (2, 4, 5, 3)
In [204]: np.allclose(x,z)
Out[204]: True
By broadcasting rules this is
(4,5,3) array - a (2,1,1,3)
(1,4,5,3)
(2,4,5,3)
The other answer, https://stackoverflow.com/a/65550381/901925, constructs:
In [205]: np.repeat(image[:,:,None], len(color_list), axis=2).shape
Out[205]: (4, 5, 2, 3)
In [206]: color_list.shape
Out[206]: (2, 3)
which by broadcasting also works (206 expands to (1,1,2,3)); he then changes the dimension order.