Skip to content
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
6 changes: 3 additions & 3 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,12 @@ def test_resize_size_equals_small_edge_size(height, width):


class TestPad:
def test_pad(self):
@pytest.mark.parametrize("fill", [85, 85.0])
def test_pad(self, fill):
height = random.randint(10, 32) * 2
width = random.randint(10, 32) * 2
img = torch.ones(3, height, width, dtype=torch.uint8)
padding = random.randint(1, 20)
fill = random.randint(1, 50)
result = transforms.Compose(
[
transforms.ToPILImage(),
Expand All @@ -484,7 +484,7 @@ def test_pad_with_tuple_of_pad_values(self):
output = transforms.Pad(padding)(img)
assert output.size == (width + padding[0] * 2, height + padding[1] * 2)

padding = tuple(random.randint(1, 20) for _ in range(4))
padding = [random.randint(1, 20) for _ in range(4)]
output = transforms.Pad(padding)(img)
assert output.size[0] == width + padding[0] + padding[2]
assert output.size[1] == height + padding[1] + padding[3]
Expand Down
8 changes: 7 additions & 1 deletion torchvision/transforms/functional_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def pad(

if not isinstance(padding, (numbers.Number, tuple, list)):
raise TypeError("Got inappropriate padding arg")
if not isinstance(fill, (numbers.Number, str, tuple)):
if not isinstance(fill, (numbers.Number, str, tuple, list)):
raise TypeError("Got inappropriate fill arg")
if not isinstance(padding_mode, str):
raise TypeError("Got inappropriate padding_mode arg")
Expand Down Expand Up @@ -301,6 +301,12 @@ def _parse_fill(

fill = tuple(fill)

if img.mode != "F":
if isinstance(fill, (list, tuple)):
fill = tuple(int(x) for x in fill)
else:
fill = int(fill)

return {name: fill}


Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def __init__(self, padding, fill=0, padding_mode="constant"):
if not isinstance(padding, (numbers.Number, tuple, list)):
raise TypeError("Got inappropriate padding arg")

if not isinstance(fill, (numbers.Number, str, tuple)):
if not isinstance(fill, (numbers.Number, str, tuple, list)):
raise TypeError("Got inappropriate fill arg")

if padding_mode not in ["constant", "edge", "reflect", "symmetric"]:
Expand Down