我要评分
获取效率
正确性
完整性
易理解

crop

Usage

Crops a region from an image following the specified coordinates of the upper left corner, width, and height of the region. It does not involve image scaling and only extracts the specified region.

Interface

1
torchvision.transforms.crop(img, top, left, height, width)

Parameters

Parameter

Description

Value Range

Input/Output

img

Input image.

torch.Tensor

Input

top

Upper coordinate of the region.

[0, image height)

Input

left

Left coordinate of the region.

[0, image width)

Input

height

Height of the region.

(0, image height – top]

Input

width

Width of the region.

(0, image width – left]

Input

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from torchvision import transforms
from PIL import Image

# Open an image.
img = Image.open('example.jpg')

# Define cropping parameters.
top = 50
left = 50
height = 100
width = 100

# Crop the image.
cropped_img = transforms.functional.crop(img, top, left, height, width)

# Save the cropped image.
cropped_img.save('cropped_example.jpg')