Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
4028d9f8
"git@developer.sourcefind.cn:OpenDAS/torchani.git" did not exist on "180633b7069958dc77afda48ab957e2127e2fa7c"
Unverified
Commit
4028d9f8
authored
Mar 17, 2024
by
Nicolas Hug
Committed by
GitHub
Mar 17, 2024
Browse files
Add pathlib.Path support for image IO utils (#8314)
parent
2bababf2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
7 deletions
+23
-7
test/test_image.py
test/test_image.py
+16
-0
torchvision/io/image.py
torchvision/io/image.py
+7
-7
No files found.
test/test_image.py
View file @
4028d9f8
...
@@ -532,5 +532,21 @@ def test_write_jpeg(img_path, tmpdir, scripted):
...
@@ -532,5 +532,21 @@ def test_write_jpeg(img_path, tmpdir, scripted):
assert_equal
(
torch_bytes
,
pil_bytes
)
assert_equal
(
torch_bytes
,
pil_bytes
)
def
test_pathlib_support
(
tmpdir
):
# Just make sure pathlib.Path is supported where relevant
jpeg_path
=
Path
(
next
(
get_images
(
ENCODE_JPEG
,
".jpg"
)))
read_file
(
jpeg_path
)
read_image
(
jpeg_path
)
write_path
=
Path
(
tmpdir
)
/
"whatever"
img
=
torch
.
randint
(
0
,
10
,
size
=
(
3
,
4
,
4
),
dtype
=
torch
.
uint8
)
write_file
(
write_path
,
data
=
img
.
flatten
())
write_jpeg
(
img
,
write_path
)
write_png
(
img
,
write_path
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
pytest
.
main
([
__file__
])
pytest
.
main
([
__file__
])
torchvision/io/image.py
View file @
4028d9f8
...
@@ -42,14 +42,14 @@ def read_file(path: str) -> torch.Tensor:
...
@@ -42,14 +42,14 @@ def read_file(path: str) -> torch.Tensor:
with one dimension.
with one dimension.
Args:
Args:
path (str): the path to the file to be read
path (str
or ``pathlib.Path``
): the path to the file to be read
Returns:
Returns:
data (Tensor)
data (Tensor)
"""
"""
if
not
torch
.
jit
.
is_scripting
()
and
not
torch
.
jit
.
is_tracing
():
if
not
torch
.
jit
.
is_scripting
()
and
not
torch
.
jit
.
is_tracing
():
_log_api_usage_once
(
read_file
)
_log_api_usage_once
(
read_file
)
data
=
torch
.
ops
.
image
.
read_file
(
path
)
data
=
torch
.
ops
.
image
.
read_file
(
str
(
path
)
)
return
data
return
data
...
@@ -59,12 +59,12 @@ def write_file(filename: str, data: torch.Tensor) -> None:
...
@@ -59,12 +59,12 @@ def write_file(filename: str, data: torch.Tensor) -> None:
file.
file.
Args:
Args:
filename (str): the path to the file to be written
filename (str
or ``pathlib.Path``
): the path to the file to be written
data (Tensor): the contents to be written to the output file
data (Tensor): the contents to be written to the output file
"""
"""
if
not
torch
.
jit
.
is_scripting
()
and
not
torch
.
jit
.
is_tracing
():
if
not
torch
.
jit
.
is_scripting
()
and
not
torch
.
jit
.
is_tracing
():
_log_api_usage_once
(
write_file
)
_log_api_usage_once
(
write_file
)
torch
.
ops
.
image
.
write_file
(
filename
,
data
)
torch
.
ops
.
image
.
write_file
(
str
(
filename
)
,
data
)
def
decode_png
(
def
decode_png
(
...
@@ -123,7 +123,7 @@ def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
...
@@ -123,7 +123,7 @@ def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
Args:
Args:
input (Tensor[channels, image_height, image_width]): int8 image tensor of
input (Tensor[channels, image_height, image_width]): int8 image tensor of
``c`` channels, where ``c`` must be 1 or 3.
``c`` channels, where ``c`` must be 1 or 3.
filename (str): Path to save the image.
filename (str
or ``pathlib.Path``
): Path to save the image.
compression_level (int): Compression factor for the resulting file, it must be a number
compression_level (int): Compression factor for the resulting file, it must be a number
between 0 and 9. Default: 6
between 0 and 9. Default: 6
"""
"""
...
@@ -211,7 +211,7 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
...
@@ -211,7 +211,7 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
Args:
Args:
input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
channels, where ``c`` must be 1 or 3.
channels, where ``c`` must be 1 or 3.
filename (str): Path to save the image.
filename (str
or ``pathlib.Path``
): Path to save the image.
quality (int): Quality of the resulting JPEG file, it must be a number
quality (int): Quality of the resulting JPEG file, it must be a number
between 1 and 100. Default: 75
between 1 and 100. Default: 75
"""
"""
...
@@ -259,7 +259,7 @@ def read_image(
...
@@ -259,7 +259,7 @@ def read_image(
The values of the output tensor are uint8 in [0, 255].
The values of the output tensor are uint8 in [0, 255].
Args:
Args:
path (str): path of the JPEG or PNG image.
path (str
or ``pathlib.Path``
): path of the JPEG or PNG image.
mode (ImageReadMode): the read mode used for optionally converting the image.
mode (ImageReadMode): the read mode used for optionally converting the image.
Default: ``ImageReadMode.UNCHANGED``.
Default: ``ImageReadMode.UNCHANGED``.
See ``ImageReadMode`` class for more information on various
See ``ImageReadMode`` class for more information on various
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment