Use Torchvision CenterCrop Transform To Do A Square Crop Of A PIL Image

Use Torchvision CenterCrop Transform (torchvision.transforms.CenterCrop) to do a square crop of a PIL image

Use Torchvision CenterCrop Transform (torchvision.transforms.CenterCrop) to do a square crop of a PIL image

Video Transcript


This video will show you how to use the Torchvision CenterCrop transform to do a square crop of a PIL image.


First, we import PyTorch.

import torch


Then we import Torchvision.

import torchvision


Next, we import torchvision.transforms as transforms.

import torchvision.transforms as transforms


Then from the Python Imaging Library, we import Image.

from PIL import Image


The image that we’re going to be using for this video example is the official Navy portrait photograph for Commodore Grace M. Hopper.


This can be found on GitHub in the following repository.

Here we see https://github.com/sebg/aiworkbox-data/tree/master/images/grace_m_hopper.

So here, you can see sebg/aiworkbox-data.

You can see the README which provides the license and description and the actual image.


Having saved it to our local directory, let’s load it into our Python environment using PIL’s image open operation.

grace_hopper_image = Image.open("grace_hopper_517x606.jpg")

So Image.open.

We’re going to pass the filename, noting here that it’s 517 pixels x 606 pixels and that’s a JPG, and we’re going to assign that to the grace_hopper_image Python variable.


Now that it’s loaded into our environment, let’s take a look at the image using PIL’s show operation.

grace_hopper_image.show()

So the Python variable grace_hopper_image.show().

Let me resize this so it fits our screen.

And there is Commodore Hopper.


Now that we have the photograph, let’s use the PyTorch Torchvision’s CenterCrop transform to do a square crop on this PIL image.


The first thing we do is define the torchvision CenterCrop transform.

pt_centercrop_transform_square = torchvision.transforms.CenterCrop(100)

So torchvision.transforms.CenterCrop and we’re going to pass in the number of 100.

Because we’re passing in only one number, the 100, it means that we want to do a CenterCrop that is 100 pixels high x 100 pixels wide.

That is, a square crop.

We’re going to assign this transform to the Python variable pt_centercrop_transform_square.


Now that we’ve defined the torchvision CenterCrop transform for a square crop, let’s see what type of thing it is.

type(pt_centercrop_transform_square)

So we use the Python type and we pass in our variable, and we see that the class is of torchvision.transforms.transforms.CenterCrop.


Now that we have our transform, let’s apply it to our original Commodore Grace Hopper image.

transformed_centercrop_square = pt_centercrop_transform_square(grace_hopper_image)

So we have our pt_centercrop_transform_square operation and to it, we’re going to pass our grace_hopper_image.

The result will be assigned to the Python variable transformed_centercrop_square.


If we just run the Python variable transformed_centercrop_square, the Python Imaging Library gives us some information about the image.

transformed_centercrop_square

Here, we can see that it’s PIL.Image.Image, and the image mode is RGB, and the size is 100x100, which is what we would expect because that’s how we define our CenterCrop transform up here.


Finally, let’s use the PIL show operation to see our newly cropped image.

transformed_centercrop_square.show()

So transformed_centercrop_square.show().

And there we have it, a square image that is 100 pixels x 100 pixels.


If we inspect the initial picture, we can see that Commodore Hopper’s chin does seem to be at the center of the picture.

So across, we see that the chin is pretty much in the center, and up and down, the chin is pretty much in the center.

So when we do a crop that is 100 pixels x 100 pixels, we start at the center point and then we figure out what the crop is.


Perfect! We were able to use the torchvision CenterCrop transform to do a square crop of a PIL image.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.