title: How do I reshape an image to NxNx3 blocks and perform operations on their channels separately
body: <p>I am trying to get a better understanding of numpy reshaping and transpose operations so that I can perform tasks on each local area of a color image (as opposed to the image as a whole). I can do these by creating slices and looping over slices, but I would prefer not having to create python loops. I have come up with some examples that should help me understand the parts that I have been having trouble with. I ordered them from easiest to most difficult. The last one is ultimately the one that I want to solve.</p>
  <pre><code>img = np.random.randint(low=0, high=256, size=(6,6,3), dtype=np.uint8)
  
  img_mean = np.mean(img) #mean of the whole image, one value.
  channel_means = np.mean(img, axis=(0,1)) #mean of each channel, three values.
  
  binarized_img = np.where(img &gt; img_mean, np.uint8(255), np.uint8(0)) #all values changed to either 0 or 255. Shape of image remains 5,5,3.
  binarized_channels = #I would like to be able to do the same as above, but by using a different mean for each channel and without using python loops.
  
  three_by_three_block_means = #I want to reshape the array into four 3x3x3 blocks and get each block's mean (should be 4 different means).
  three_by_three_block_channel_means = #Same as above, but this time I want the mean of each channel of each block (should be 12 different means).
  #I also want to be able to change the block's size arbitrarily, i.e. from 3x3x3 blocks to 2x2x3 blocks when needed.
  
  binarized_blocks = #same as binarized_img, but done separately for each block based on their means instead of the mean of the whole image.
  binarized_block_channels = #same as binarized_blocks, but done separately for each channel in each block.
  </code></pre>
  <p>If someone could show me how to complete these examples using only numpy (no python loops), I could learn from them and use them to accomplish the (similar) tasks that I frequently have trouble with.</p>