"git@developer.sourcefind.cn:ox696c/ktransformers.git" did not exist on "c05ebb74b1a04376cc4f7863a66efec1457bdede"
Commit e8d3291f authored by Philip Meier's avatar Philip Meier Committed by Francisco Massa
Browse files

Fixed doc of crop functionals (#1388)

parent b9cbc227
...@@ -349,46 +349,53 @@ def pad(img, padding, fill=0, padding_mode='constant'): ...@@ -349,46 +349,53 @@ def pad(img, padding, fill=0, padding_mode='constant'):
return Image.fromarray(img) return Image.fromarray(img)
def crop(img, i, j, h, w): def crop(img, top, left, height, width):
"""Crop the given PIL Image. """Crop the given PIL Image.
Args: Args:
img (PIL Image): Image to be cropped. img (PIL Image): Image to be cropped. (0,0) denotes the top left corner of the image.
i (int): i in (i,j) i.e coordinates of the upper left corner. top (int): Vertical component of the top left corner of the crop box.
j (int): j in (i,j) i.e coordinates of the upper left corner. left (int): Horizontal component of the top left corner of the crop box.
h (int): Height of the cropped image. height (int): Height of the crop box.
w (int): Width of the cropped image. width (int): Width of the crop box.
Returns: Returns:
PIL Image: Cropped image. PIL Image: Cropped image.
""" """
if not _is_pil_image(img): if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img))) raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
return img.crop((j, i, j + w, i + h)) return img.crop((left, top, left + width, top + height))
def center_crop(img, output_size): def center_crop(img, output_size):
"""Crop the given PIL Image and resize it to desired size.
Args:
img (PIL Image): Image to be cropped. (0,0) denotes the top left corner of the image.
output_size (sequence or int): (height, width) of the crop box. If int,
it is used for both directions
Returns:
PIL Image: Cropped image.
"""
if isinstance(output_size, numbers.Number): if isinstance(output_size, numbers.Number):
output_size = (int(output_size), int(output_size)) output_size = (int(output_size), int(output_size))
w, h = img.size image_width, image_height = img.size
th, tw = output_size crop_height, crop_width = output_size
i = int(round((h - th) / 2.)) crop_top = int(round((image_height - crop_height) / 2.))
j = int(round((w - tw) / 2.)) crop_left = int(round((image_width - crop_width) / 2.))
return crop(img, i, j, th, tw) return crop(img, crop_top, crop_left, crop_height, crop_width)
def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR): def resized_crop(img, top, left, height, width, size, interpolation=Image.BILINEAR):
"""Crop the given PIL Image and resize it to desired size. """Crop the given PIL Image and resize it to desired size.
Notably used in :class:`~torchvision.transforms.RandomResizedCrop`. Notably used in :class:`~torchvision.transforms.RandomResizedCrop`.
Args: Args:
img (PIL Image): Image to be cropped. img (PIL Image): Image to be cropped. (0,0) denotes the top left corner of the image.
i (int): i in (i,j) i.e coordinates of the upper left corner top (int): Vertical component of the top left corner of the crop box.
j (int): j in (i,j) i.e coordinates of the upper left corner left (int): Horizontal component of the top left corner of the crop box.
h (int): Height of the cropped image. height (int): Height of the crop box.
w (int): Width of the cropped image. width (int): Width of the crop box.
size (sequence or int): Desired output size. Same semantics as ``resize``. size (sequence or int): Desired output size. Same semantics as ``resize``.
interpolation (int, optional): Desired interpolation. Default is interpolation (int, optional): Desired interpolation. Default is
``PIL.Image.BILINEAR``. ``PIL.Image.BILINEAR``.
...@@ -396,7 +403,7 @@ def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR): ...@@ -396,7 +403,7 @@ def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
PIL Image: Cropped image. PIL Image: Cropped image.
""" """
assert _is_pil_image(img), 'img should be PIL Image' assert _is_pil_image(img), 'img should be PIL Image'
img = crop(img, i, j, h, w) img = crop(img, top, left, height, width)
img = resize(img, size, interpolation) img = resize(img, size, interpolation)
return img return img
...@@ -495,16 +502,18 @@ def five_crop(img, size): ...@@ -495,16 +502,18 @@ def five_crop(img, size):
else: else:
assert len(size) == 2, "Please provide only two dimensions (h, w) for size." assert len(size) == 2, "Please provide only two dimensions (h, w) for size."
w, h = img.size image_width, image_height = img.size
crop_h, crop_w = size crop_height, crop_width = size
if crop_w > w or crop_h > h: if crop_width > image_width or crop_height > image_height:
raise ValueError("Requested crop size {} is bigger than input size {}".format(size, msg = "Requested crop size {} is bigger than input size {}"
(h, w))) raise ValueError(msg.format(size, (image_height, image_width)))
tl = img.crop((0, 0, crop_w, crop_h))
tr = img.crop((w - crop_w, 0, w, crop_h)) tl = img.crop((0, 0, crop_width, crop_height))
bl = img.crop((0, h - crop_h, crop_w, h)) tr = img.crop((image_width - crop_width, 0, image_width, crop_height))
br = img.crop((w - crop_w, h - crop_h, w, h)) bl = img.crop((0, image_height - crop_height, crop_width, image_height))
center = center_crop(img, (crop_h, crop_w)) br = img.crop((image_width - crop_width, image_height - crop_height,
image_width, image_height))
center = center_crop(img, (crop_height, crop_width))
return (tl, tr, bl, br, center) return (tl, tr, bl, br, center)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment