Python code to extract patches from an image, do some operation, and unpatchify it. Supports overlapping patches.
def PatchProcess(img, patch_size=64, stride=64, paddingMultiple=128):
"""
Applies per-patch operations to an image with automatic padding and reconstruction
for fixed-size convolutional network compatibility.
This function takes a 2D input image, pads it to dimensions that are multiples of 128
(typically required by specific CNN architectures or tiling constraints), extracts patches
using sliding windows, applies a transformation to each patch, reconstructs the image
using torch.nn.functional.fold, and finally crops the image back to its original dimensions.
Note: The default operation inside the processing loop is an identity transformation
(patch * 1.0). This function serves as a template for implementing custom per-patch
logic, such as applying a neural network, filtering, or other transformations.
Parameters
----------
img : numpy.ndarray
Input image array of shape (H, W) in standard Python format.
Values can be float32, float64, or uint8 (converted internally).
patch_size : int, optional
Size of the patches for sliding window extraction. Default is 64.
stride : int, optional
Stride step for extracting overlapping or non-overlapping patches.
Default is 64.
paddingMultiple : int, optional
The multiple to which the image dimensions will be padded. Default is 128.
For example, if image is 500x500, it will be padded to 512x512 since its the smallest multiple of 128 that is greater than or equal to 500.
Returns
-------
numpy.ndarray
The processed image array restored to its original dimensions and data type (float32).
Output shape matches the input shape (H, W).
"""
return img_output