insert_shape

Inserts sub-image into a larger image at the specified location.

Import packages

[1]:
import matplotlib.pyplot as plt
import numpy as np
import porespy as ps

ps.visualization.set_mpl_style()
np.random.seed(10)

Create image and element:

[2]:
image = np.random.randint(1, 3, size=(5, 5, 5))
element = np.random.randint(10, 11, size=(3, 3, 3))
print(image)
print(element)
[[[2 2 1 2 1]
  [2 2 1 2 2]
  [1 2 2 1 1]
  [2 1 1 1 1]
  [1 2 1 1 2]]

 [[2 1 1 2 1]
  [1 2 1 1 1]
  [2 2 1 2 2]
  [2 2 2 1 2]
  [1 1 1 1 2]]

 [[1 2 2 2 1]
  [2 1 2 2 1]
  [2 1 1 2 1]
  [1 1 2 2 1]
  [1 1 2 1 2]]

 [[2 1 2 2 1]
  [2 1 1 1 1]
  [1 2 1 2 1]
  [1 1 1 1 1]
  [1 2 2 1 2]]

 [[2 1 1 2 1]
  [1 2 1 1 1]
  [1 1 1 2 2]
  [2 1 1 1 2]
  [2 2 2 1 2]]]
[[[10 10 10]
  [10 10 10]
  [10 10 10]]

 [[10 10 10]
  [10 10 10]
  [10 10 10]]

 [[10 10 10]
  [10 10 10]
  [10 10 10]]]

The image array and shape array are both arrays of the same dimensions, in this case 3-dimensional arrays.

Apply generator function:

With different parameters

Corner

[3]:
im = ps.generators.insert_shape(im=image,
                                element=element,
                                center=None,
                                corner=[0, 0, 0],
                                value=1,
                                mode="overwrite")
print(im)
[[[10 10 10  2  1]
  [10 10 10  2  2]
  [10 10 10  1  1]
  [ 2  1  1  1  1]
  [ 1  2  1  1  2]]

 [[10 10 10  2  1]
  [10 10 10  1  1]
  [10 10 10  2  2]
  [ 2  2  2  1  2]
  [ 1  1  1  1  2]]

 [[10 10 10  2  1]
  [10 10 10  2  1]
  [10 10 10  2  1]
  [ 1  1  2  2  1]
  [ 1  1  2  1  2]]

 [[ 2  1  2  2  1]
  [ 2  1  1  1  1]
  [ 1  2  1  2  1]
  [ 1  1  1  1  1]
  [ 1  2  2  1  2]]

 [[ 2  1  1  2  1]
  [ 1  2  1  1  1]
  [ 1  1  1  2  2]
  [ 2  1  1  1  2]
  [ 2  2  2  1  2]]]

The element is anchored in the [0, 0, 0] corner.

Center

[4]:
im = ps.generators.insert_shape(im=image,
                                element=element,
                                center=[2, 3, 2],
                                corner=None,
                                value=1,
                                mode="overwrite")
print(im)
[[[ 2  2  1  2  1]
  [ 2  2  1  2  2]
  [ 1  2  2  1  1]
  [ 2  1  1  1  1]
  [ 1  2  1  1  2]]

 [[ 2  1  1  2  1]
  [ 1  2  1  1  1]
  [ 2 10 10 10  2]
  [ 2 10 10 10  2]
  [ 1 10 10 10  2]]

 [[ 1  2  2  2  1]
  [ 2  1  2  2  1]
  [ 2 10 10 10  1]
  [ 1 10 10 10  1]
  [ 1 10 10 10  2]]

 [[ 2  1  2  2  1]
  [ 2  1  1  1  1]
  [ 1 10 10 10  1]
  [ 1 10 10 10  1]
  [ 1 10 10 10  2]]

 [[ 2  1  1  2  1]
  [ 1  2  1  1  1]
  [ 1  1  1  2  2]
  [ 2  1  1  1  2]
  [ 2  2  2  1  2]]]

The image is centered at [2, 3, 2].

Value

[5]:
im = ps.generators.insert_shape(im=image,
                                element=element,
                                center=[2, 2, 2],
                                corner=None,
                                value=5,
                                mode="overwrite")
print(im)
[[[ 2  2  1  2  1]
  [ 2  2  1  2  2]
  [ 1  2  2  1  1]
  [ 2  1  1  1  1]
  [ 1  2  1  1  2]]

 [[ 2  1  1  2  1]
  [ 1 50 50 50  1]
  [ 2 50 50 50  2]
  [ 2 50 50 50  2]
  [ 1  1  1  1  2]]

 [[ 1  2  2  2  1]
  [ 2 50 50 50  1]
  [ 2 50 50 50  1]
  [ 1 50 50 50  1]
  [ 1  1  2  1  2]]

 [[ 2  1  2  2  1]
  [ 2 50 50 50  1]
  [ 1 50 50 50  1]
  [ 1 50 50 50  1]
  [ 1  2  2  1  2]]

 [[ 2  1  1  2  1]
  [ 1  2  1  1  1]
  [ 1  1  1  2  2]
  [ 2  1  1  1  2]
  [ 2  2  2  1  2]]]

The element was multiplied by value (5), before being inserted to the image.

Mode

[6]:
im = ps.generators.insert_shape(im=image,
                                element=element,
                                center=[2, 2, 2],
                                corner=None,
                                value=1,
                                mode="overwrite")
print(im)
[[[ 2  2  1  2  1]
  [ 2  2  1  2  2]
  [ 1  2  2  1  1]
  [ 2  1  1  1  1]
  [ 1  2  1  1  2]]

 [[ 2  1  1  2  1]
  [ 1 10 10 10  1]
  [ 2 10 10 10  2]
  [ 2 10 10 10  2]
  [ 1  1  1  1  2]]

 [[ 1  2  2  2  1]
  [ 2 10 10 10  1]
  [ 2 10 10 10  1]
  [ 1 10 10 10  1]
  [ 1  1  2  1  2]]

 [[ 2  1  2  2  1]
  [ 2 10 10 10  1]
  [ 1 10 10 10  1]
  [ 1 10 10 10  1]
  [ 1  2  2  1  2]]

 [[ 2  1  1  2  1]
  [ 1  2  1  1  1]
  [ 1  1  1  2  2]
  [ 2  1  1  1  2]
  [ 2  2  2  1  2]]]

The element has been inserted to the image. Since the mode was “overwrite” the specified section of the image is replaced by the element.

[7]:
im = ps.generators.insert_shape(im=image,
                                element=element,
                                center=[2, 2, 2],
                                corner=None,
                                value=1,
                                mode="overlay")
print(im)
[[[ 2  2  1  2  1]
  [ 2  2  1  2  2]
  [ 1  2  2  1  1]
  [ 2  1  1  1  1]
  [ 1  2  1  1  2]]

 [[ 2  1  1  2  1]
  [ 1 12 11 11  1]
  [ 2 12 11 12  2]
  [ 2 12 12 11  2]
  [ 1  1  1  1  2]]

 [[ 1  2  2  2  1]
  [ 2 11 12 12  1]
  [ 2 11 11 12  1]
  [ 1 11 12 12  1]
  [ 1  1  2  1  2]]

 [[ 2  1  2  2  1]
  [ 2 11 11 11  1]
  [ 1 12 11 12  1]
  [ 1 11 11 11  1]
  [ 1  2  2  1  2]]

 [[ 2  1  1  2  1]
  [ 1  2  1  1  1]
  [ 1  1  1  2  2]
  [ 2  1  1  1  2]
  [ 2  2  2  1  2]]]

The element has been inserted to the image. Since the mode was “overlay” the values of the element were added to the values of the image.