@@ -277,13 +277,13 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
277
277
278
278
279
279
def decode_image (
280
- input : torch .Tensor ,
280
+ input : Union [ torch .Tensor , str ] ,
281
281
mode : ImageReadMode = ImageReadMode .UNCHANGED ,
282
282
apply_exif_orientation : bool = False ,
283
283
) -> torch .Tensor :
284
- """
285
- Detect whether an image is a JPEG, PNG, WEBP, or GIF and performs the
286
- appropriate operation to decode the image into a Tensor .
284
+ """Decode an image into a tensor.
285
+
286
+ Currently supported image formats are jpeg, png, gif and webp .
287
287
288
288
The values of the output tensor are in uint8 in [0, 255] for most cases.
289
289
@@ -295,8 +295,9 @@ def decode_image(
295
295
tensor.
296
296
297
297
Args:
298
- input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the
299
- image.
298
+ input (Tensor or str or ``pathlib.Path``): The image to decode. If a
299
+ tensor is passed, it must be one dimensional uint8 tensor containing
300
+ the raw bytes of the image. Otherwise, this must be a path to the image file.
300
301
mode (ImageReadMode): the read mode used for optionally converting the image.
301
302
Default: ``ImageReadMode.UNCHANGED``.
302
303
See ``ImageReadMode`` class for more information on various
@@ -309,6 +310,8 @@ def decode_image(
309
310
"""
310
311
if not torch .jit .is_scripting () and not torch .jit .is_tracing ():
311
312
_log_api_usage_once (decode_image )
313
+ if not isinstance (input , torch .Tensor ):
314
+ input = read_file (str (input ))
312
315
output = torch .ops .image .decode_image (input , mode .value , apply_exif_orientation )
313
316
return output
314
317
@@ -318,30 +321,7 @@ def read_image(
318
321
mode : ImageReadMode = ImageReadMode .UNCHANGED ,
319
322
apply_exif_orientation : bool = False ,
320
323
) -> torch .Tensor :
321
- """
322
- Reads a JPEG, PNG, WEBP, or GIF image into a Tensor.
323
-
324
- The values of the output tensor are in uint8 in [0, 255] for most cases.
325
-
326
- If the image is a 16-bit png, then the output tensor is uint16 in [0, 65535]
327
- (supported from torchvision ``0.21``. Since uint16 support is limited in
328
- pytorch, we recommend calling
329
- :func:`torchvision.transforms.v2.functional.to_dtype()` with ``scale=True``
330
- after this function to convert the decoded image into a uint8 or float
331
- tensor.
332
-
333
- Args:
334
- path (str or ``pathlib.Path``): path of the image.
335
- mode (ImageReadMode): the read mode used for optionally converting the image.
336
- Default: ``ImageReadMode.UNCHANGED``.
337
- See ``ImageReadMode`` class for more information on various
338
- available modes. Only applies to JPEG and PNG images.
339
- apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
340
- Only applies to JPEG and PNG images. Default: False.
341
-
342
- Returns:
343
- output (Tensor[image_channels, image_height, image_width])
344
- """
324
+ """[OBSOLETE] Use :func:`~torchvision.io.decode_image` instead."""
345
325
if not torch .jit .is_scripting () and not torch .jit .is_tracing ():
346
326
_log_api_usage_once (read_image )
347
327
data = read_file (path )
0 commit comments