I have two 3D numpy arrays, each representing an image (x, y, rgb). I want to write the smaller image into the larger image, except for any magenta pixels (255, 0, 255). I know how to generate a 2D mask representing all the magenta pixels -- how can I use this 2D mask in a 3D array operation?
1 Answer
Here is one example. It's not precisely accurate (all values are one or two), but gives the idea. Hopefully this works in your situation:
image1 = np.ones((5,5,16))
image2 = 2 * np.ones((3,3,16))
mask = np.array([[0, 1, 1], [0, 0, 1], [1, 0, 0]])
x, y = np.where(mask)
image1[1:4,1:4,:][x,y,:] = image2[x,y,:]
(1:4,1:4 is the position of the subimage inside the larger image.)
1 Comment
Tom Quinn
For those trying to use this in numpypy, it won't work -- apparently 1-arg where isn't implemented yet. Is there another method that pypy has a better chance of supporting?