From 3db94920cd201732554879ab8ef1c0f64b5dcbd3 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 30 Aug 2019 11:32:49 +0200 Subject: [PATCH 1/2] Adds optional fill colour to rotate --- torchvision/transforms/functional.py | 9 +++++++-- torchvision/transforms/transforms.py | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index fc07e402db8..6c20efdfa1f 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -686,7 +686,7 @@ def adjust_gamma(img, gamma, gain=1): return img -def rotate(img, angle, resample=False, expand=False, center=None): +def rotate(img, angle, resample=False, expand=False, center=None, fill=0): """Rotate the image by angle. @@ -703,6 +703,8 @@ def rotate(img, angle, resample=False, expand=False, center=None): center (2-tuple, optional): Optional center of rotation. Origin is the upper left corner. Default is the center of the image. + fill (3-tuple or int): RGB pixel fill value for area outside the rotated image. + If int, it is used for all channels respectively. .. _filters: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#filters @@ -711,7 +713,10 @@ def rotate(img, angle, resample=False, expand=False, center=None): if not _is_pil_image(img): raise TypeError('img should be PIL Image. Got {}'.format(type(img))) - return img.rotate(angle, resample, expand, center) + if isinstance(fill, int): + fill = tuple([int] * 3) + + return img.rotate(angle, resample, expand, center, fillcolor=fill) def _get_inverse_affine_matrix(center, angle, translate, scale, shear): diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index 203dae345cd..b21a6d86eef 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -946,12 +946,14 @@ class RandomRotation(object): center (2-tuple, optional): Optional center of rotation. Origin is the upper left corner. Default is the center of the image. + fill (3-tuple or int): RGB pixel fill value for area outside the rotated image. + If int, it is used for all channels respectively. .. _filters: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#filters """ - def __init__(self, degrees, resample=False, expand=False, center=None): + def __init__(self, degrees, resample=False, expand=False, center=None, fill=0): if isinstance(degrees, numbers.Number): if degrees < 0: raise ValueError("If degrees is a single number, it must be positive.") @@ -964,6 +966,7 @@ def __init__(self, degrees, resample=False, expand=False, center=None): self.resample = resample self.expand = expand self.center = center + self.fill = fill @staticmethod def get_params(degrees): @@ -987,7 +990,7 @@ def __call__(self, img): angle = self.get_params(self.degrees) - return F.rotate(img, angle, self.resample, self.expand, self.center) + return F.rotate(img, angle, self.resample, self.expand, self.center, self.fill) def __repr__(self): format_string = self.__class__.__name__ + '(degrees={0}'.format(self.degrees) From e70c4e3a1259c253c19d35fb67947a1e439e7a9d Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Fri, 30 Aug 2019 12:19:44 +0200 Subject: [PATCH 2/2] bug fix --- torchvision/transforms/functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py index 6c20efdfa1f..72da9b7889d 100644 --- a/torchvision/transforms/functional.py +++ b/torchvision/transforms/functional.py @@ -714,7 +714,7 @@ def rotate(img, angle, resample=False, expand=False, center=None, fill=0): raise TypeError('img should be PIL Image. Got {}'.format(type(img))) if isinstance(fill, int): - fill = tuple([int] * 3) + fill = tuple([fill] * 3) return img.rotate(angle, resample, expand, center, fillcolor=fill)