Skip to content

F.pad is broken for PIL images and fill: str #5627

Closed
@pmeier

Description

@pmeier

On the surface F.pad for PIL images seems to support str arguments for fill:

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

Internally though, it calls _parse_fill

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

which in most cases relies on fill being numeric. Thus, actually passing a string doesn't work although PIL supports it.

>>> image = PIL.Image.fromarray(torch.randint(0, 256, (256, 256, 3), dtype=torch.uint8).numpy())
>>> F.pad(image, 128, fill="black")
ValueError: invalid literal for int() with base 10: 'black'
>>> PIL.ImageOps.expand(image, border=128, fill="black")
<PIL.Image.Image image mode=RGB size=512x512 at 0x7FA4DD37FBD0>

This traces back to #2284 and specifically to

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)))

which will makes no sense for fill: str.

Given that #2284 was included from 0.7.0 upwards and we haven't gotten any report yet, my guess is that the functionality is not really used. Since we want to deprecate fill: str anyway (#5621 (comment)), I would opt to simply remove all mentions of it. Thoughts?

cc @vfdev-5 @datumbox

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions