'Clipping a .tiff Raster into grids with Python

I have a large .tiff raster (at least 200x150), but I need a bunch of 32x32 .tiff files. Is there an easy way to cut up that .tiff in Python? I think the workflow would look something like:

  1. Create 32x32 box in bottom right corner of raster
  2. Clip raster with that box, saving new clipped .tiff raster
  3. Shift box left by 32 pixels (if less than 32 pixels left, shift up 32 pixels and restart on right side)
  4. Repeat clip/shift until can't shift up or sideways

The input raster won't be an even multiple of 32, but I don't care if I lose some of the original raster off of the sides. As long as the original data is preserved for each 32x32 raster, I'm happy.



Solution 1:[1]

I was able to solve this using the arcpy module. The arcpy.management.SplitRaster() documentation is available at https://pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/split-raster.htm. After this, I used os.listdir() to get a list of the output files. OpenCV has a ndarrayt.shape() function that gives the dimensions of the image, so I looped that over each file and deleted any that didn't match the size that I wanted.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SwiftestKoala