Replacing Part of 2D Array with Another 2D Array in Numpy

Explanation with examples

Alex P
4 min readDec 9, 2021

In this article I’ll show a couple of simple and powerful techniques for working with arrays in Python.

For example, we have 7x7 array:

Simple replacement

Suppose that we need to replace the part of 7x7 array, which is highlighted with green color, with another 2x4 array:

Create a new notebook in Jupyter Notebook. Import Numpy library and create arrays a, a_copy and b:

Output:

Array a:
[[1 1 1 1 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 1 1 1 1 1 1]]

Copy of array a:
[[1 1 1 1 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 1 1 1 1 1 1]]

Array b:
[[8 5 5 8]
[8 5 5 8]]

Let’s replace elements in array a in the 4th & 5th rows and in the 3rd, 4th, 5th & 6th columns with array b:

Output:

Array a after being partially replaced with array b:
[[1 1 1 1 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 8 5 5 8 1]
[1 4 8 5 5 8 1]
[1 4 4 4 1 1 1]
[1 1 1 1 1 1 1]]

Well, that was easy! A short line of code a[3:5,2:6] = b did the job!

Conditional replacement with help of boolean mask

Now, suppose that we need to do the same replacement, but with one condition. Only those values in array a which are overlapped with number 5 from array b should be replaced:

We will do that with help of boolean mask.

Boolean mask can be easily retrieved with this code b_mask_boolean = b == 5. Let’s see how it looks:

Output:

Boolean mask for numbers equal to 5 in array b:
[[False True True False]
[False True True False]]

We can do cool things with help of boolean mask. For example, assign zero values to the elements of array, in which we are not interested in, or vice versa:

Output:

Array b multiplied by boolean mask:
[[0 5 5 0]
[0 5 5 0]]

Array b multiplied by inversed boolean mask:
[[8 0 0 8]
[8 0 0 8]]

Part of array a (4th and 5th rows; 3rd, 4th, 5th and 6th columns) multiplied by boolean mask:
[[0 4 1 0]
[0 4 1 0]]

Part of array a (4th and 5th rows; 3rd, 4th, 5th and 6th columns) multiplied by inversed boolean mask:
[[4 0 0 1]
[4 0 0 1]]

Let’s do the replacement:

Output:

Array a after being partly replaced with array b (with numbers 5 from array b only):
[[1 1 1 1 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 4 1 1 1]
[1 4 4 5 5 1 1]
[1 4 4 5 5 1 1]
[1 4 4 4 1 1 1]
[1 1 1 1 1 1 1]]

Essentially we have added up two these arrays:

[[4 0 0 1]
[4 0 0 1]]

and

[[0 5 5 0]
[0 5 5 0]]

to get this array

[[4 5 5 1]
[4 5 5 1]]

and replaced with this array the elements in array a in the 4th & 5th rows and in the 3rd, 4th, 5th & 6th columns.

Again, this solution was easy too! A line of code a_copy[3:5,2:6] = a_copy[3:5,2:6] * ~b_mask_boolean + b * b_mask_boolean did the job.

Real example

Here you can see how the described technique can be applied for images.

We have two images (background and a cup of coffee):

We can add a cup of coffee to the background in a simple way:

Or using a boolean mask:

However, you should remember, when we work with images, we work with 3D arrays, because, besides the dimensions related to height and width, there is an additional dimension related to RGB channels. Read more details in the article “Adding Objects to Image in Python”.

--

--

Alex P

Machine learning engineer, computer vision enthusiast