Skip to content

Adds optional fill colour to rotate #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions torchvision/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand All @@ -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

Expand All @@ -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([fill] * 3)

return img.rotate(angle, resample, expand, center, fillcolor=fill)


def _get_inverse_affine_matrix(center, angle, translate, scale, shear):
Expand Down
7 changes: 5 additions & 2 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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):
Expand All @@ -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)
Expand Down