Skip to content

Commit

Permalink
Pad updated (pytorch#683)
Browse files Browse the repository at this point in the history
* pad updated

* checked pad

* pad checked

* pad checked
  • Loading branch information
surgan12 authored and fmassa committed Dec 13, 2018
1 parent 8bd05e6 commit 3884faf
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions torchvision/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ def pad(img, padding, fill=0, padding_mode='constant'):
'Padding mode should be either constant, edge, reflect or symmetric'

if padding_mode == 'constant':
if img.mode == 'P':
palette = img.getpalette()
image = ImageOps.expand(img, border=padding, fill=fill)
image.putpalette(palette)
return image

return ImageOps.expand(img, border=padding, fill=fill)
else:
if isinstance(padding, int):
Expand All @@ -301,6 +307,14 @@ def pad(img, padding, fill=0, padding_mode='constant'):
pad_right = padding[2]
pad_bottom = padding[3]

if img.mode == 'P':
palette = img.getpalette()
img = np.asarray(img)
img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode)
img = Image.fromarray(img)
img.putpalette(palette)
return img

img = np.asarray(img)
# RGB image
if len(img.shape) == 3:
Expand Down

0 comments on commit 3884faf

Please sign in to comment.