20
20
21
21
22
22
class ImageReadMode (Enum ):
23
- """
24
- Support for various modes while reading images.
23
+ """Allow automatic conversion to RGB, RGBA, etc while decoding.
24
+
25
+ .. note::
26
+
27
+ You don't need to use this struct, you can just pass strings to all
28
+ ``mode`` parameters, e.g. ``mode="RGB"``.
25
29
26
- Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,
27
- ``ImageReadMode.GRAY`` for converting to grayscale,
28
- ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,
29
- ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for
30
- RGB with transparency.
30
+ The different available modes are the following.
31
+
32
+ - UNCHANGED: loads the image as-is
33
+ - RGB: converts to RGB
34
+ - RGBA: converts to RGB with transparency (also aliased as RGB_ALPHA)
35
+ - GRAY: converts to grayscale
36
+ - GRAY_ALPHA: converts to grayscale with transparency
31
37
32
38
.. note::
33
39
34
- Some decoders won't support all possible values, e.g. a decoder may only
35
- support "RGB" and "RGBA" mode .
40
+ Some decoders won't support all possible values, e.g. GRAY and
41
+ GRAY_ALPHA are only supported for PNG and JPEG images .
36
42
"""
37
43
38
44
UNCHANGED = 0
@@ -45,8 +51,7 @@ class ImageReadMode(Enum):
45
51
46
52
def read_file (path : str ) -> torch .Tensor :
47
53
"""
48
- Reads and outputs the bytes contents of a file as a uint8 Tensor
49
- with one dimension.
54
+ Return the bytes contents of a file as a uint8 1D Tensor.
50
55
51
56
Args:
52
57
path (str or ``pathlib.Path``): the path to the file to be read
@@ -62,8 +67,7 @@ def read_file(path: str) -> torch.Tensor:
62
67
63
68
def write_file (filename : str , data : torch .Tensor ) -> None :
64
69
"""
65
- Writes the contents of an uint8 tensor with one dimension to a
66
- file.
70
+ Write the content of an uint8 1D tensor to a file.
67
71
68
72
Args:
69
73
filename (str or ``pathlib.Path``): the path to the file to be written
@@ -93,10 +97,9 @@ def decode_png(
93
97
Args:
94
98
input (Tensor[1]): a one dimensional uint8 tensor containing
95
99
the raw bytes of the PNG image.
96
- mode (str or ImageReadMode): the read mode used for optionally
97
- converting the image. Default: ``ImageReadMode.UNCHANGED``.
98
- See `ImageReadMode` class for more information on various
99
- available modes.
100
+ mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
101
+ Default is "UNCHANGED". See :class:`~torchvision.io.ImageReadMode`
102
+ for available modes.
100
103
apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
101
104
Default: False.
102
105
@@ -156,8 +159,7 @@ def decode_jpeg(
156
159
device : Union [str , torch .device ] = "cpu" ,
157
160
apply_exif_orientation : bool = False ,
158
161
) -> Union [torch .Tensor , List [torch .Tensor ]]:
159
- """
160
- Decode JPEG image(s) into 3 dimensional RGB or grayscale Tensor(s).
162
+ """Decode JPEG image(s) into 3D RGB or grayscale Tensor(s), on CPU or CUDA.
161
163
162
164
The values of the output tensor are uint8 between 0 and 255.
163
165
@@ -171,12 +173,9 @@ def decode_jpeg(
171
173
input (Tensor[1] or list[Tensor[1]]): a (list of) one dimensional uint8 tensor(s) containing
172
174
the raw bytes of the JPEG image. The tensor(s) must be on CPU,
173
175
regardless of the ``device`` parameter.
174
- mode (str or ImageReadMode): the read mode used for optionally
175
- converting the image(s). The supported modes are: ``ImageReadMode.UNCHANGED``,
176
- ``ImageReadMode.GRAY`` and ``ImageReadMode.RGB``
177
- Default: ``ImageReadMode.UNCHANGED``.
178
- See ``ImageReadMode`` class for more information on various
179
- available modes.
176
+ mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
177
+ Default is "UNCHANGED". See :class:`~torchvision.io.ImageReadMode`
178
+ for available modes.
180
179
device (str or torch.device): The device on which the decoded image will
181
180
be stored. If a cuda device is specified, the image will be decoded
182
181
with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only
@@ -228,9 +227,7 @@ def decode_jpeg(
228
227
def encode_jpeg (
229
228
input : Union [torch .Tensor , List [torch .Tensor ]], quality : int = 75
230
229
) -> Union [torch .Tensor , List [torch .Tensor ]]:
231
- """
232
- Takes a (list of) input tensor(s) in CHW layout and returns a (list of) buffer(s) with the contents
233
- of the corresponding JPEG file(s).
230
+ """Encode RGB tensor(s) into raw encoded jpeg bytes, on CPU or CUDA.
234
231
235
232
.. note::
236
233
Passing a list of CUDA tensors is more efficient than repeated individual calls to ``encode_jpeg``.
@@ -286,7 +283,7 @@ def decode_image(
286
283
mode : ImageReadMode = ImageReadMode .UNCHANGED ,
287
284
apply_exif_orientation : bool = False ,
288
285
) -> torch .Tensor :
289
- """Decode an image into a tensor.
286
+ """Decode an image into a uint8 tensor, from a path or from raw encoded bytes .
290
287
291
288
Currently supported image formats are jpeg, png, gif and webp.
292
289
@@ -303,10 +300,9 @@ def decode_image(
303
300
input (Tensor or str or ``pathlib.Path``): The image to decode. If a
304
301
tensor is passed, it must be one dimensional uint8 tensor containing
305
302
the raw bytes of the image. Otherwise, this must be a path to the image file.
306
- mode (str or ImageReadMode): the read mode used for optionally converting the image.
307
- Default: ``ImageReadMode.UNCHANGED``.
308
- See ``ImageReadMode`` class for more information on various
309
- available modes. Only applies to JPEG and PNG images.
303
+ mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
304
+ Default is "UNCHANGED". See :class:`~torchvision.io.ImageReadMode`
305
+ for available modes.
310
306
apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
311
307
Only applies to JPEG and PNG images. Default: False.
312
308
@@ -367,9 +363,9 @@ def decode_webp(
367
363
Args:
368
364
input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
369
365
the raw bytes of the WEBP image.
370
- mode (str or ImageReadMode): The read mode used for optionally
371
- converting the image color space. Default: ``ImageReadMode.UNCHANGED``.
372
- Other supported values are ``ImageReadMode.RGB`` and ``ImageReadMode.RGB_ALPHA`` .
366
+ mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
367
+ Default is "UNCHANGED". See :class:`~torchvision.io.ImageReadMode`
368
+ for available modes .
373
369
374
370
Returns:
375
371
Decoded image (Tensor[image_channels, image_height, image_width])
@@ -398,9 +394,9 @@ def _decode_avif(
398
394
Args:
399
395
input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
400
396
the raw bytes of the AVIF image.
401
- mode (str or ImageReadMode): The read mode used for optionally
402
- converting the image color space. Default: ``ImageReadMode.UNCHANGED``.
403
- Other supported values are ``ImageReadMode.RGB`` and ``ImageReadMode.RGB_ALPHA`` .
397
+ mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
398
+ Default is "UNCHANGED". See :class:`~torchvision.io.ImageReadMode`
399
+ for available modes .
404
400
405
401
Returns:
406
402
Decoded image (Tensor[image_channels, image_height, image_width])
@@ -426,9 +422,9 @@ def _decode_heic(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN
426
422
Args:
427
423
input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
428
424
the raw bytes of the HEIC image.
429
- mode (str or ImageReadMode): The read mode used for optionally
430
- converting the image color space. Default: ``ImageReadMode.UNCHANGED``.
431
- Other supported values are ``ImageReadMode.RGB`` and ``ImageReadMode.RGB_ALPHA`` .
425
+ mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
426
+ Default is "UNCHANGED". See :class:`~torchvision.io.ImageReadMode`
427
+ for available modes .
432
428
433
429
Returns:
434
430
Decoded image (Tensor[image_channels, image_height, image_width])
0 commit comments