Torchvision.transforms.functional.pad with pillow

Padding pillow image with torchvision.transforms.functional.pad` fails.

w, h = image.size
max_wh = np.max([w, h])
hp = int((max_wh - w) / 2)
vp = int((max_wh - h) / 2)
padding = (hp, vp, hp, vp)
print(F.pad(image, padding, 0, 'constant'))

This gives me this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-49-022af53bc969> in <module>
      5 vp = int((max_wh - h) / 2)
      6 padding = (hp, vp, hp, vp)
----> 7 print(F.pad(image, padding, 0, 'constant'))

/nas/env/work/lib/python3.8/site-packages/torch/nn/functional.py in _pad(input, pad, mode, value)
   3548                 _pad, (input,), input, pad, mode=mode, value=value)
   3549     assert len(pad) % 2 == 0, 'Padding length must be divisible by 2'
-> 3550     assert len(pad) // 2 <= input.dim(), 'Padding length too large'
   3551     if mode == 'constant':
   3552         return _VF.constant_pad_nd(input, pad, value)

AttributeError: 'PngImageFile' object has no attribute 'dim'

Per `torchvision.transforms.functional. pad’ docs, pad should take in pillow image. What is going wrong here?

look at trace. you are calling torch.nn.functional.pad