Skip to content

Commit 4028d9f

Browse files
authored
Add pathlib.Path support for image IO utils (#8314)
1 parent 2bababf commit 4028d9f

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

test/test_image.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,21 @@ def test_write_jpeg(img_path, tmpdir, scripted):
532532
assert_equal(torch_bytes, pil_bytes)
533533

534534

535+
def test_pathlib_support(tmpdir):
536+
# Just make sure pathlib.Path is supported where relevant
537+
538+
jpeg_path = Path(next(get_images(ENCODE_JPEG, ".jpg")))
539+
540+
read_file(jpeg_path)
541+
read_image(jpeg_path)
542+
543+
write_path = Path(tmpdir) / "whatever"
544+
img = torch.randint(0, 10, size=(3, 4, 4), dtype=torch.uint8)
545+
546+
write_file(write_path, data=img.flatten())
547+
write_jpeg(img, write_path)
548+
write_png(img, write_path)
549+
550+
535551
if __name__ == "__main__":
536552
pytest.main([__file__])

torchvision/io/image.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def read_file(path: str) -> torch.Tensor:
4242
with one dimension.
4343
4444
Args:
45-
path (str): the path to the file to be read
45+
path (str or ``pathlib.Path``): the path to the file to be read
4646
4747
Returns:
4848
data (Tensor)
4949
"""
5050
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
5151
_log_api_usage_once(read_file)
52-
data = torch.ops.image.read_file(path)
52+
data = torch.ops.image.read_file(str(path))
5353
return data
5454

5555

@@ -59,12 +59,12 @@ def write_file(filename: str, data: torch.Tensor) -> None:
5959
file.
6060
6161
Args:
62-
filename (str): the path to the file to be written
62+
filename (str or ``pathlib.Path``): the path to the file to be written
6363
data (Tensor): the contents to be written to the output file
6464
"""
6565
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
6666
_log_api_usage_once(write_file)
67-
torch.ops.image.write_file(filename, data)
67+
torch.ops.image.write_file(str(filename), data)
6868

6969

7070
def decode_png(
@@ -123,7 +123,7 @@ def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
123123
Args:
124124
input (Tensor[channels, image_height, image_width]): int8 image tensor of
125125
``c`` channels, where ``c`` must be 1 or 3.
126-
filename (str): Path to save the image.
126+
filename (str or ``pathlib.Path``): Path to save the image.
127127
compression_level (int): Compression factor for the resulting file, it must be a number
128128
between 0 and 9. Default: 6
129129
"""
@@ -211,7 +211,7 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
211211
Args:
212212
input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
213213
channels, where ``c`` must be 1 or 3.
214-
filename (str): Path to save the image.
214+
filename (str or ``pathlib.Path``): Path to save the image.
215215
quality (int): Quality of the resulting JPEG file, it must be a number
216216
between 1 and 100. Default: 75
217217
"""
@@ -259,7 +259,7 @@ def read_image(
259259
The values of the output tensor are uint8 in [0, 255].
260260
261261
Args:
262-
path (str): path of the JPEG or PNG image.
262+
path (str or ``pathlib.Path``): path of the JPEG or PNG image.
263263
mode (ImageReadMode): the read mode used for optionally converting the image.
264264
Default: ``ImageReadMode.UNCHANGED``.
265265
See ``ImageReadMode`` class for more information on various

0 commit comments

Comments
 (0)