0

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 1

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

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?

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.