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
e4d2d1ad
Unverified
Commit
e4d2d1ad
authored
May 08, 2024
by
Nicolas Hug
Committed by
GitHub
May 08, 2024
Browse files
Add GIF decoder (#8406)
parent
1644fff3
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
6 deletions
+27
-6
torchvision/io/__init__.py
torchvision/io/__init__.py
+1
-0
torchvision/io/image.py
torchvision/io/image.py
+26
-6
No files found.
torchvision/io/__init__.py
View file @
e4d2d1ad
...
@@ -21,6 +21,7 @@ from ._video_opt import (
...
@@ -21,6 +21,7 @@ from ._video_opt import (
VideoMetaData
,
VideoMetaData
,
)
)
from
.image
import
(
from
.image
import
(
decode_gif
,
decode_image
,
decode_image
,
decode_jpeg
,
decode_jpeg
,
decode_png
,
decode_png
,
...
...
torchvision/io/image.py
View file @
e4d2d1ad
...
@@ -225,7 +225,7 @@ def decode_image(
...
@@ -225,7 +225,7 @@ def decode_image(
input
:
torch
.
Tensor
,
mode
:
ImageReadMode
=
ImageReadMode
.
UNCHANGED
,
apply_exif_orientation
:
bool
=
False
input
:
torch
.
Tensor
,
mode
:
ImageReadMode
=
ImageReadMode
.
UNCHANGED
,
apply_exif_orientation
:
bool
=
False
)
->
torch
.
Tensor
:
)
->
torch
.
Tensor
:
"""
"""
Detect
s
whether an image is a JPEG or
PNG
and performs the appropriate
Detect whether an image is a JPEG
, PNG
or
GIF
and performs the appropriate
operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
Optionally converts the image to the desired format.
Optionally converts the image to the desired format.
...
@@ -237,9 +237,9 @@ def decode_image(
...
@@ -237,9 +237,9 @@ def decode_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
available modes.
available modes.
Ignored for GIFs.
apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
Default: False.
Ignored for GIFs.
Default: False.
Returns:
Returns:
output (Tensor[image_channels, image_height, image_width])
output (Tensor[image_channels, image_height, image_width])
...
@@ -254,7 +254,7 @@ def read_image(
...
@@ -254,7 +254,7 @@ def read_image(
path
:
str
,
mode
:
ImageReadMode
=
ImageReadMode
.
UNCHANGED
,
apply_exif_orientation
:
bool
=
False
path
:
str
,
mode
:
ImageReadMode
=
ImageReadMode
.
UNCHANGED
,
apply_exif_orientation
:
bool
=
False
)
->
torch
.
Tensor
:
)
->
torch
.
Tensor
:
"""
"""
Reads a JPEG or
PNG
image into a 3 dimensional RGB or grayscale Tensor.
Reads a JPEG
, PNG
or
GIF
image into a 3 dimensional RGB or grayscale Tensor.
Optionally converts the image to the desired format.
Optionally converts the image to the desired format.
The values of the output tensor are uint8 in [0, 255].
The values of the output tensor are uint8 in [0, 255].
...
@@ -263,9 +263,9 @@ def read_image(
...
@@ -263,9 +263,9 @@ def read_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
available modes.
available modes.
Ignored for GIFs.
apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
Default: False.
Ignored for GIFs.
Default: False.
Returns:
Returns:
output (Tensor[image_channels, image_height, image_width])
output (Tensor[image_channels, image_height, image_width])
...
@@ -279,3 +279,23 @@ def read_image(
...
@@ -279,3 +279,23 @@ def read_image(
def
_read_png_16
(
path
:
str
,
mode
:
ImageReadMode
=
ImageReadMode
.
UNCHANGED
)
->
torch
.
Tensor
:
def
_read_png_16
(
path
:
str
,
mode
:
ImageReadMode
=
ImageReadMode
.
UNCHANGED
)
->
torch
.
Tensor
:
data
=
read_file
(
path
)
data
=
read_file
(
path
)
return
torch
.
ops
.
image
.
decode_png
(
data
,
mode
.
value
,
True
)
return
torch
.
ops
.
image
.
decode_png
(
data
,
mode
.
value
,
True
)
def
decode_gif
(
input
:
torch
.
Tensor
)
->
torch
.
Tensor
:
"""
Decode a GIF image into a 3 or 4 dimensional RGB Tensor.
The values of the output tensor are uint8 between 0 and 255.
The output tensor has shape ``(C, H, W)`` if there is only one image in the
GIF, and ``(N, C, H, W)`` if there are ``N`` images.
Args:
input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
the raw bytes of the GIF image.
Returns:
output (Tensor[image_channels, image_height, image_width] or Tensor[num_images, image_channels, image_height, image_width])
"""
if
not
torch
.
jit
.
is_scripting
()
and
not
torch
.
jit
.
is_tracing
():
_log_api_usage_once
(
decode_gif
)
return
torch
.
ops
.
image
.
decode_gif
(
input
)
Prev
1
2
Next
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