I’m using numpy to build pixel arrays. An 800×600 image is an 3-dimensional array of uint8, 800x600x3. I also have a similar array with a fixed pattern (a checkerboard, see here). I have another array, 800×600 of mask values. Where the mask is zero, I want to copy the pattern pixel to the image pixel. Where the mask is not zero, I want to leave the image pixel alone.
>>> image.shape
(800, 600, 3)
>>> chex.shape
(800, 600, 3)
>>> mask.shape
(800, 600)
This feels like it should work:
image[mask == 0,...] = chex
but gives “ValueError: array is not broadcastable to correct shape”.
What do I use to copy chex pixels to image pixels where mask is zero?
Note that
imagehas shape (800,600,3), whileidxhas shape (800,600). The rules for indexing stateThus indexing arrays have a sort of broadcasting ability of their own.
idx‘s shape gets promoted to (800,600,:)