@@ -349,54 +349,61 @@ def pad(img, padding, fill=0, padding_mode='constant'):
349
349
return Image .fromarray (img )
350
350
351
351
352
- def crop (img , i , j , h , w ):
352
+ def crop (img , top , left , height , width ):
353
353
"""Crop the given PIL Image.
354
-
355
354
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.
362
360
Returns:
363
361
PIL Image: Cropped image.
364
362
"""
365
363
if not _is_pil_image (img ):
366
364
raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
367
365
368
- return img .crop ((j , i , j + w , i + h ))
366
+ return img .crop ((left , top , left + width , top + height ))
369
367
370
368
371
369
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
+ """
372
379
if isinstance (output_size , numbers .Number ):
373
380
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 )
379
386
380
387
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 ):
382
389
"""Crop the given PIL Image and resize it to desired size.
383
390
384
391
Notably used in :class:`~torchvision.transforms.RandomResizedCrop`.
385
392
386
393
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 .
392
399
size (sequence or int): Desired output size. Same semantics as ``resize``.
393
400
interpolation (int, optional): Desired interpolation. Default is
394
401
``PIL.Image.BILINEAR``.
395
402
Returns:
396
403
PIL Image: Cropped image.
397
404
"""
398
405
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 )
400
407
img = resize (img , size , interpolation )
401
408
return img
402
409
@@ -495,16 +502,18 @@ def five_crop(img, size):
495
502
else :
496
503
assert len (size ) == 2 , "Please provide only two dimensions (h, w) for size."
497
504
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 ))
508
517
return (tl , tr , bl , br , center )
509
518
510
519
0 commit comments