Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
37799adc
Unverified
Commit
37799adc
authored
Jul 30, 2020
by
Philip Meier
Committed by
GitHub
Jul 30, 2020
Browse files
Fix fill parameter for PIL pad (#2515)
* fix fill parameter for PIL pad * add test * fix * lint
parent
300ef76d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
11 deletions
+15
-11
test/test_transforms.py
test/test_transforms.py
+8
-0
torchvision/transforms/functional_pil.py
torchvision/transforms/functional_pil.py
+7
-11
No files found.
test/test_transforms.py
View file @
37799adc
...
...
@@ -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
)
...
...
torchvision/transforms/functional_pil.py
View file @
37799adc
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment