Unverified Commit 37799adc authored by Philip Meier's avatar Philip Meier Committed by GitHub
Browse files

Fix fill parameter for PIL pad (#2515)

* fix fill parameter for PIL pad

* add test

* fix

* lint
parent 300ef76d
......@@ -390,6 +390,14 @@ class Tester(unittest.TestCase):
with self.assertRaises(ValueError):
transforms.Pad((1, 2, 3, 4, 5))
def test_pad_with_mode_F_images(self):
pad = 2
transform = transforms.Pad(pad)
img = Image.new("F", (10, 10))
padded_img = transform(img)
self.assertSequenceEqual(padded_img.size, [edge_size + 2 * pad for edge_size in img.size])
def test_lambda(self):
trans = transforms.Lambda(lambda x: x.add(10))
x = torch.randn(10)
......
......@@ -261,19 +261,14 @@ def pad(img, padding, fill=0, padding_mode="constant"):
raise ValueError("Padding mode should be either constant, edge, reflect or symmetric")
if padding_mode == "constant":
if isinstance(fill, numbers.Number):
fill = (fill,) * len(img.getbands())
if len(fill) != len(img.getbands()):
raise ValueError("fill should have the same number of elements "
"as the number of channels in the image "
"({}), got {} instead".format(len(img.getbands()), len(fill)))
opts = _parse_fill(fill, img, "2.3.0", name="fill")
if img.mode == "P":
palette = img.getpalette()
image = ImageOps.expand(img, border=padding, fill=fill)
image = ImageOps.expand(img, border=padding, **opts)
image.putpalette(palette)
return image
return ImageOps.expand(img, border=padding, fill=fill)
return ImageOps.expand(img, border=padding, **opts)
else:
if isinstance(padding, int):
pad_left = pad_right = pad_top = pad_bottom = padding
......@@ -367,8 +362,8 @@ def resize(img, size, interpolation=Image.BILINEAR):
@torch.jit.unused
def _parse_fill(fill, img, min_pil_version):
"""Helper function to get the fill color for rotate and perspective transforms.
def _parse_fill(fill, img, min_pil_version, name="fillcolor"):
"""Helper function to get the fill color for rotate, perspective transforms, and pad.
Args:
fill (n-tuple or int or float): Pixel fill value for area outside the transformed
......@@ -377,6 +372,7 @@ def _parse_fill(fill, img, min_pil_version):
img (PIL Image): Image to be filled.
min_pil_version (str): The minimum PILLOW version for when the ``fillcolor`` option
was first introduced in the calling function. (e.g. rotate->5.2.0, perspective->5.0.0)
name (str): Name of the ``fillcolor`` option in the output. Defaults to ``"fillcolor"``.
Returns:
dict: kwarg for ``fillcolor``
......@@ -401,7 +397,7 @@ def _parse_fill(fill, img, min_pil_version):
"bands of the image ({} != {})")
raise ValueError(msg.format(len(fill), num_bands))
return {"fillcolor": fill}
return {name: fill}
@torch.jit.unused
......
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