Patchify in Python – To Extract Patches from Large Images

In this article, we’ll look at how to extract patches from large pictures using Python.

We prefer to train any deep learning algorithm with tiny images since they generate better results. But what if we have very large images? One possibility is to break the larger photographs into smaller patches, which would allow us to train any algorithm.

You’re probably wondering what patch means. An image patch, as the name implies, is a collection of pixels in a photograph. Let’s say I have a 20 by 20-pixel image. It can be broken into 1000 2 *2 pixel square patches.

Patchify in Python:

patchify is a Python tool for cropping pictures and saving the cropped or patched images in a Numpy array.

Patchify can break an image into small overlapping pieces based on the patch unit size supplied, and then fuse the areas back together with the original image.

The patchify module is used to transform photos to image patches, whereas Numpy is used to build image data.

But first, use the pip command to ensure that patchify is installed on your system.

pip install patchify

Output:

Collecting patchify Downloading patchify-0.2.3-py3-none-any.whl (6.6 kB)
Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.7/
dist-packages (from patchify) (1.19.5) Installing collected packages: 
patchify Successfully installed patchify-0.2.3

Patchify in Python – To Extract Patches from Large Images

 

Method #1: Using patchify Module(Static Input)

Approach:

  • Import the numpy library as np using the import keyword.
  • Import patchify function from patchify module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Create an array using the np.array() method for creating the image data by passing the given list as an argument and store it in another variable.
  • Print the shape of the image data obtained using the shape method.
  • Pass the above image, pixel size, step value as the arguments to the patchify() function for extracting the patches from the above image.
  • Store it in another variable.
  • Print all the patches of the image using the shape method.
  • The Exit of the Program.

Below is the implementation:

# Import the numpy library as np using the import keyword.
import numpy as np
# Import patchify function from patchify module using the import keyword.
from patchify import patchify
# Give the list as static input and store it in a variable.
gvn_list = [[35, 45, 55, 65], [20, 25, 75, 45],
            [100, 15, 12, 90], [200, 300, 400, 10], [12, 40, 50, 60]]
# Create an array using the np.array() method for creating the image data
# by passing the given list as an argument.
# store it in another variable.
rslt_img = np.array(gvn_list)
# Print the shape of the image data obtained using the shape method.
print(rslt_img.shape)
# Pass the above image, pixel size, step value as the arguments to the patchify() function
# for extracting the patches from the above image.
# Store it in another variable.
rsltpatchs = patchify(rslt_img, (2, 2), step=2)
# Print all the patches of the image using the shape method.
print(rsltpatchs.shape)

Output:

(5, 4)
(2, 2, 2, 2)