Skip to content

Commit e8d3291

Browse files
Philip Meierfmassa
Philip Meier
authored andcommitted
Fixed doc of crop functionals (#1388)
1 parent b9cbc22 commit e8d3291

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

torchvision/transforms/functional.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -349,54 +349,61 @@ def pad(img, padding, fill=0, padding_mode='constant'):
349349
return Image.fromarray(img)
350350

351351

352-
def crop(img, i, j, h, w):
352+
def crop(img, top, left, height, width):
353353
"""Crop the given PIL Image.
354-
355354
Args:
356-
img (PIL Image): Image to be cropped.
357-
i (int): i in (i,j) i.e coordinates of the upper left corner.
358-
j (int): j in (i,j) i.e coordinates of the upper left corner.
359-
h (int): Height of the cropped image.
360-
w (int): Width of the cropped image.
361-
355+
img (PIL Image): Image to be cropped. (0,0) denotes the top left corner of the image.
356+
top (int): Vertical component of the top left corner of the crop box.
357+
left (int): Horizontal component of the top left corner of the crop box.
358+
height (int): Height of the crop box.
359+
width (int): Width of the crop box.
362360
Returns:
363361
PIL Image: Cropped image.
364362
"""
365363
if not _is_pil_image(img):
366364
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
367365

368-
return img.crop((j, i, j + w, i + h))
366+
return img.crop((left, top, left + width, top + height))
369367

370368

371369
def center_crop(img, output_size):
370+
"""Crop the given PIL Image and resize it to desired size.
371+
372+
Args:
373+
img (PIL Image): Image to be cropped. (0,0) denotes the top left corner of the image.
374+
output_size (sequence or int): (height, width) of the crop box. If int,
375+
it is used for both directions
376+
Returns:
377+
PIL Image: Cropped image.
378+
"""
372379
if isinstance(output_size, numbers.Number):
373380
output_size = (int(output_size), int(output_size))
374-
w, h = img.size
375-
th, tw = output_size
376-
i = int(round((h - th) / 2.))
377-
j = int(round((w - tw) / 2.))
378-
return crop(img, i, j, th, tw)
381+
image_width, image_height = img.size
382+
crop_height, crop_width = output_size
383+
crop_top = int(round((image_height - crop_height) / 2.))
384+
crop_left = int(round((image_width - crop_width) / 2.))
385+
return crop(img, crop_top, crop_left, crop_height, crop_width)
379386

380387

381-
def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
388+
def resized_crop(img, top, left, height, width, size, interpolation=Image.BILINEAR):
382389
"""Crop the given PIL Image and resize it to desired size.
383390
384391
Notably used in :class:`~torchvision.transforms.RandomResizedCrop`.
385392
386393
Args:
387-
img (PIL Image): Image to be cropped.
388-
i (int): i in (i,j) i.e coordinates of the upper left corner
389-
j (int): j in (i,j) i.e coordinates of the upper left corner
390-
h (int): Height of the cropped image.
391-
w (int): Width of the cropped image.
394+
img (PIL Image): Image to be cropped. (0,0) denotes the top left corner of the image.
395+
top (int): Vertical component of the top left corner of the crop box.
396+
left (int): Horizontal component of the top left corner of the crop box.
397+
height (int): Height of the crop box.
398+
width (int): Width of the crop box.
392399
size (sequence or int): Desired output size. Same semantics as ``resize``.
393400
interpolation (int, optional): Desired interpolation. Default is
394401
``PIL.Image.BILINEAR``.
395402
Returns:
396403
PIL Image: Cropped image.
397404
"""
398405
assert _is_pil_image(img), 'img should be PIL Image'
399-
img = crop(img, i, j, h, w)
406+
img = crop(img, top, left, height, width)
400407
img = resize(img, size, interpolation)
401408
return img
402409

@@ -495,16 +502,18 @@ def five_crop(img, size):
495502
else:
496503
assert len(size) == 2, "Please provide only two dimensions (h, w) for size."
497504

498-
w, h = img.size
499-
crop_h, crop_w = size
500-
if crop_w > w or crop_h > h:
501-
raise ValueError("Requested crop size {} is bigger than input size {}".format(size,
502-
(h, w)))
503-
tl = img.crop((0, 0, crop_w, crop_h))
504-
tr = img.crop((w - crop_w, 0, w, crop_h))
505-
bl = img.crop((0, h - crop_h, crop_w, h))
506-
br = img.crop((w - crop_w, h - crop_h, w, h))
507-
center = center_crop(img, (crop_h, crop_w))
505+
image_width, image_height = img.size
506+
crop_height, crop_width = size
507+
if crop_width > image_width or crop_height > image_height:
508+
msg = "Requested crop size {} is bigger than input size {}"
509+
raise ValueError(msg.format(size, (image_height, image_width)))
510+
511+
tl = img.crop((0, 0, crop_width, crop_height))
512+
tr = img.crop((image_width - crop_width, 0, image_width, crop_height))
513+
bl = img.crop((0, image_height - crop_height, crop_width, image_height))
514+
br = img.crop((image_width - crop_width, image_height - crop_height,
515+
image_width, image_height))
516+
center = center_crop(img, (crop_height, crop_width))
508517
return (tl, tr, bl, br, center)
509518

510519

0 commit comments

Comments
 (0)