In [1]:
import numpy as np

In [2]:
import skimage.util

In [3]:
a = np.arange(24).reshape(4, 6)
print(a)


[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

In [4]:
blocks = skimage.util.view_as_blocks(a, (2, 3))
print(blocks)


[[[[ 0  1  2]
   [ 6  7  8]]

  [[ 3  4  5]
   [ 9 10 11]]]


 [[[12 13 14]
   [18 19 20]]

  [[15 16 17]
   [21 22 23]]]]

In [5]:
print(type(blocks))


<class 'numpy.ndarray'>

In [6]:
print(blocks.shape)


(2, 2, 2, 3)

In [7]:
print(blocks[0, 0])


[[0 1 2]
 [6 7 8]]

In [8]:
print(blocks[0, 1])


[[ 3  4  5]
 [ 9 10 11]]

In [9]:
print(blocks[1, 0])


[[12 13 14]
 [18 19 20]]

In [10]:
print(blocks[1, 1])


[[15 16 17]
 [21 22 23]]

In [11]:
# blocks = skimage.util.view_as_blocks(a, (2, 4))
# ValueError: 'block_shape' is not compatible with 'arr_in'

In [12]:
print(np.shares_memory(a, blocks))


True

In [13]:
a[0, 0] = 100

In [14]:
print(blocks[0, 0])


[[100   1   2]
 [  6   7   8]]

In [15]:
a = np.arange(24).reshape(4, 6)
blocks_copy = skimage.util.view_as_blocks(a, (2, 3)).copy()

In [16]:
print(np.shares_memory(a, blocks_copy))


False

In [17]:
a[0, 0] = 100

In [18]:
print(blocks_copy[0, 0])


[[0 1 2]
 [6 7 8]]

In [19]:
blocks_copy2 = skimage.util.view_as_blocks(a.copy(), (2, 3))

In [20]:
print(np.shares_memory(a, blocks_copy2))


False