code: def extractblocks(img, blksz, stpsz=None):
      
  
      # See http://stackoverflow.com/questions/16774148 and
      # sklearn.feature_extraction.image.extract_patches_2d
      if isinstance(img, tuple):
          img = np.stack(img, axis=-1)
  
      if stpsz is None:
          stpsz = (1,) * len(blksz)
  
      imgsz = img.shape
  
      # Calculate the number of blocks that can fit in each dimension of
      # the images
      numblocks = tuple(int(np.floor((a - b) / c) + 1) for a, b, c in
                        zip_longest(imgsz, blksz, stpsz, fillvalue=1))
  
      # Calculate the strides for blocks
      blockstrides = tuple(a * b for a, b in zip_longest(img.strides, stpsz,
                                                         fillvalue=1))
  
      new_shape = blksz + numblocks
      new_strides = img.strides[:len(blksz)] + blockstrides
      blks = np.lib.stride_tricks.as_strided(img, new_shape, new_strides)
      return np.reshape(blks, blksz + (-1,))
docs: Extract blocks from an ndarray signal into an ndarray.
  
      Parameters
      ----------
      img : ndarray or tuple of ndarrays
        nd array of images, or tuple of images
      blksz : tuple
        tuple of block sizes, blocks are taken starting from the first index
        of img
      stpsz : tuple, optional (default None, corresponds to steps of 1)
        tuple of step sizes between neighboring blocks
  
      Returns
      -------
      blks : ndarray
        image blocks