Reshape a 2D image into a collection of patches.
The resulting patches are allocated in a dedicated array.
Read more in the User Guide.
The original image data. For color images, the last dimension specifies the channel: a RGB image would have n_channels=3.
The dimensions of one patch.
The maximum number of patches to extract. If max_patches is a float between 0 and 1, it is taken to be a proportion of the total number of patches. If max_patches is None it corresponds to the total number of patches that can be extracted.
Determines the random number generator used for random sampling when max_patches is not None. Use an int to make the randomness deterministic. See Glossary.
The collection of patches extracted from the image, where n_patches is either max_patches or the total number of patches that can be extracted.
>>> from sklearn.datasets import load_sample_image
>>> from sklearn.feature_extraction import image
>>> # Use the array data from the first image in this dataset:
>>> one_image = load_sample_image("china.jpg")
>>> print('Image shape: {}'.format(one_image.shape))
Image shape: (427, 640, 3)
>>> patches = image.extract_patches_2d(one_image, (2, 2))
>>> print('Patches shape: {}'.format(patches.shape))
Patches shape: (272214, 2, 2, 3)
>>> # Here are just two of these patches:
>>> print(patches[1])
[[[174 201 231]
[174 201 231]]
[[173 200 230]
[173 200 230]]]
>>> print(patches[800])
[[[187 214 243]
[188 215 244]]
[[187 214 243]
[188 215 244]]]
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.feature_extraction.image.extract_patches_2d.html