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

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

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

Video Transcript


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


First, we import Pytorch.

import torch


Then we check the PyTorch version we are using.

print(torch.__version__)

We are using PyTorch 0.4.0.


Next, we import Torchvision.

import torchvision


Then we check the Torchvision version we are using.

print(torchvision.__version__)

We are using Torchvision version 0.2.1.


Then from the Python Imaging Library (PIL), 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 github.com/sebg/aiworkbox-data.

When you click on here, you'll see that I go into the images folder, and then the grace_m_hopper folder.

When I scroll down, you'll see that there's a README and then there's the image that we're going to be using, which is 517 pixels x 606 pixels.


Having saved it down into our local directory, let's load it into our Python environment using PIL's Image.open.

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

So here, we can see the Grace Hopper image that we just referenced.

Then we're going to assign this to the Python variable grace_hopper_image.


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

grace_hopper_image.show()

Let me resize it for us, and there is Commodore Hopper.


Now that we have the photograph, we're going to use PyTorch Torchvision CenterCrop transform to do a rectangular crop of this PIL image.

pt_centercrop_transform_rectangle = torchvision.transforms.CenterCrop((300,50))

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

So here, we see torchvision.transforms.CenterCrop, and we're going to pass in two numbers, (300, 50).

Note that there is a parenthesis to start and a parenthesis to end.

So we're passing in a list of two numbers to the CenterCrop operation.

Then all of this is going to be assigned to the Python variable pt_centercrop_transform_rectangle.


Let's check the transform to see what it gives us and we see that it is a CenterCrop and the size is going to be 300 x 50.

pt_centercrop_transform_rectangle

The first number specifies the height, so it's going to be 300 pixels high.

And the second number specifies the width, so it's going to be 50 pixels wide.

So CenterCrop is going to return to us an image that is cropped that is 300 pixels high by 50 pixels wide.


Let's now apply this to our original Commodore Grace Hopper image.

centercrop_rectangle = pt_centercrop_transform_rectangle(grace_hopper_image)

So we have the Python variable pt_centercrop_transform_rectangle and we're going to pass in our grace_hopper_image to it and we're going to assign what this returns to the Python variable centercrop_rectangle.


If we just evaluate the centercrop_rectangle Python variable, we can see that it's a PIL image, we see that the mode is RGB, we see that the size is 50x300, and it’s at some address.

centercrop_rectangle

The interesting thing here is that the numbers have been reversed.

So when we initially defined our CenterCrop, it's height and then width.

With the PIL Image, it's width and then height.


Finally, let's use the PIL show operation on our centercrop_rectangle to actually view the image that was created.

centercrop_rectangle.show()

There we have it, a rectangular image that is 300 pixels high by 50 pixels wide.


If we go back to the original image and we say here is the center, we're going 300 pixels high and 50 pixels wide, which is why we see this.


Perfect! We were able to use the Torchvision CenterCrop transform to do a rectangular crop of a PIL image using the height and width that we wanted to specify.

Receive the Data Science Weekly Newsletter every Thursday

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