`F.pad` is broken for PIL images and `fill: str`
See original GitHub issueOn the surface F.pad
for PIL images seems to support str arguments for fill
:
Internally though, it calls _parse_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
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 (https://github.com/pytorch/vision/pull/5621#discussion_r827271131), I would opt to simply remove all mentions of it. Thoughts?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to pad an image on all sides in PyTorch? - GeeksforGeeks
Pad () method is used for padding an image. This method accepts images like PIL Image and Tensor Image. The tensor image is...
Read more >torchvision.transforms - PyTorch
All transformations accept PIL Image, Tensor Image or batch of Tensor Images as ... constant: pads with a constant value, this value is...
Read more >Saving a Numpy array as an image - python - Stack Overflow
An answer using PIL (just in case it's useful). given a numpy array "A": from PIL import Image im = Image.fromarray(A) im.save("your_file.jpeg").
Read more >PIL.TiffImagePlugin - Pillow (PIL Fork) 9.3.0 documentation
Source code for PIL.TiffImagePlugin. # # The Python Imaging Library. # $Id$ # # TIFF file handling # # TIFF is a flexible,...
Read more >Pillow 2.2.1 - PyPI
Pillow. Python Imaging Library (Fork). Pillow is the “friendly” PIL fork by Alex Clark and Contributors. PIL is the Python Imaging Library by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Looking again for all
str
mentions, the only additional mention of astr
representing a numeric value isRandomErasing
as you found. Still,value: str
is actually not correct here. It should bevalue: Literal["random"]
:https://github.com/pytorch/vision/blob/9edd22c8a07a8c99324a73ec4dd6c553e2c8d410/torchvision/transforms/transforms.py#L1653-L1656
That’s indeed a very convenient bug. We should remove it rather than fix it, I agree. Nevertheless we should consider removing/deprecating string colours from the transforms not only for pad but also for all other transforms as these won’t be compatible with the new API for non-PIL images.