We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Loading 16bit png images
ported here from https://github.com/pytorch/pytorch/issues/32971

Original description:

When I was trying  to load 16 bit .png grayscale image with torchvision.datasets.imagefolder ,it is loading every image as white only. 
I solved this issue by doing transformation operations outside Compose function.


cc @pmeier @wanifarooq @choidongyeon 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchvision/io/image.py`
Content:
```
1 from enum import Enum
2 
3 import torch
4 
5 from .._internally_replaced_utils import _get_extension_path
6 
7 
8 try:
9     lib_path = _get_extension_path("image")
10     torch.ops.load_library(lib_path)
11 except (ImportError, OSError):
12     pass
13 
14 
15 class ImageReadMode(Enum):
16     """
17     Support for various modes while reading images.
18 
19     Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,
20     ``ImageReadMode.GRAY`` for converting to grayscale,
21     ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,
22     ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for
23     RGB with transparency.
24     """
25 
26     UNCHANGED = 0
27     GRAY = 1
28     GRAY_ALPHA = 2
29     RGB = 3
30     RGB_ALPHA = 4
31 
32 
33 def read_file(path: str) -> torch.Tensor:
34     """
35     Reads and outputs the bytes contents of a file as a uint8 Tensor
36     with one dimension.
37 
38     Args:
39         path (str): the path to the file to be read
40 
41     Returns:
42         data (Tensor)
43     """
44     data = torch.ops.image.read_file(path)
45     return data
46 
47 
48 def write_file(filename: str, data: torch.Tensor) -> None:
49     """
50     Writes the contents of a uint8 tensor with one dimension to a
51     file.
52 
53     Args:
54         filename (str): the path to the file to be written
55         data (Tensor): the contents to be written to the output file
56     """
57     torch.ops.image.write_file(filename, data)
58 
59 
60 def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
61     """
62     Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
63     Optionally converts the image to the desired format.
64     The values of the output tensor are uint8 between 0 and 255.
65 
66     Args:
67         input (Tensor[1]): a one dimensional uint8 tensor containing
68             the raw bytes of the PNG image.
69         mode (ImageReadMode): the read mode used for optionally
70             converting the image. Default: ``ImageReadMode.UNCHANGED``.
71             See `ImageReadMode` class for more information on various
72             available modes.
73 
74     Returns:
75         output (Tensor[image_channels, image_height, image_width])
76     """
77     output = torch.ops.image.decode_png(input, mode.value)
78     return output
79 
80 
81 def encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor:
82     """
83     Takes an input tensor in CHW layout and returns a buffer with the contents
84     of its corresponding PNG file.
85 
86     Args:
87         input (Tensor[channels, image_height, image_width]): int8 image tensor of
88             ``c`` channels, where ``c`` must 3 or 1.
89         compression_level (int): Compression factor for the resulting file, it must be a number
90             between 0 and 9. Default: 6
91 
92     Returns:
93         Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the
94             PNG file.
95     """
96     output = torch.ops.image.encode_png(input, compression_level)
97     return output
98 
99 
100 def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
101     """
102     Takes an input tensor in CHW layout (or HW in the case of grayscale images)
103     and saves it in a PNG file.
104 
105     Args:
106         input (Tensor[channels, image_height, image_width]): int8 image tensor of
107             ``c`` channels, where ``c`` must be 1 or 3.
108         filename (str): Path to save the image.
109         compression_level (int): Compression factor for the resulting file, it must be a number
110             between 0 and 9. Default: 6
111     """
112     output = encode_png(input, compression_level)
113     write_file(filename, output)
114 
115 
116 def decode_jpeg(
117     input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = "cpu"
118 ) -> torch.Tensor:
119     """
120     Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.
121     Optionally converts the image to the desired format.
122     The values of the output tensor are uint8 between 0 and 255.
123 
124     Args:
125         input (Tensor[1]): a one dimensional uint8 tensor containing
126             the raw bytes of the JPEG image. This tensor must be on CPU,
127             regardless of the ``device`` parameter.
128         mode (ImageReadMode): the read mode used for optionally
129             converting the image. Default: ``ImageReadMode.UNCHANGED``.
130             See ``ImageReadMode`` class for more information on various
131             available modes.
132         device (str or torch.device): The device on which the decoded image will
133             be stored. If a cuda device is specified, the image will be decoded
134             with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only
135             supported for CUDA version >= 10.1
136 
137     Returns:
138         output (Tensor[image_channels, image_height, image_width])
139     """
140     device = torch.device(device)
141     if device.type == "cuda":
142         output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)
143     else:
144         output = torch.ops.image.decode_jpeg(input, mode.value)
145     return output
146 
147 
148 def encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor:
149     """
150     Takes an input tensor in CHW layout and returns a buffer with the contents
151     of its corresponding JPEG file.
152 
153     Args:
154         input (Tensor[channels, image_height, image_width])): int8 image tensor of
155             ``c`` channels, where ``c`` must be 1 or 3.
156         quality (int): Quality of the resulting JPEG file, it must be a number between
157             1 and 100. Default: 75
158 
159     Returns:
160         output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the
161             JPEG file.
162     """
163     if quality < 1 or quality > 100:
164         raise ValueError("Image quality should be a positive number " "between 1 and 100")
165 
166     output = torch.ops.image.encode_jpeg(input, quality)
167     return output
168 
169 
170 def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
171     """
172     Takes an input tensor in CHW layout and saves it in a JPEG file.
173 
174     Args:
175         input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
176             channels, where ``c`` must be 1 or 3.
177         filename (str): Path to save the image.
178         quality (int): Quality of the resulting JPEG file, it must be a number
179             between 1 and 100. Default: 75
180     """
181     output = encode_jpeg(input, quality)
182     write_file(filename, output)
183 
184 
185 def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
186     """
187     Detects whether an image is a JPEG or PNG and performs the appropriate
188     operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
189 
190     Optionally converts the image to the desired format.
191     The values of the output tensor are uint8 between 0 and 255.
192 
193     Args:
194         input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the
195             PNG or JPEG image.
196         mode (ImageReadMode): the read mode used for optionally converting the image.
197             Default: ``ImageReadMode.UNCHANGED``.
198             See ``ImageReadMode`` class for more information on various
199             available modes.
200 
201     Returns:
202         output (Tensor[image_channels, image_height, image_width])
203     """
204     output = torch.ops.image.decode_image(input, mode.value)
205     return output
206 
207 
208 def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
209     """
210     Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.
211     Optionally converts the image to the desired format.
212     The values of the output tensor are uint8 between 0 and 255.
213 
214     Args:
215         path (str): path of the JPEG or PNG image.
216         mode (ImageReadMode): the read mode used for optionally converting the image.
217             Default: ``ImageReadMode.UNCHANGED``.
218             See ``ImageReadMode`` class for more information on various
219             available modes.
220 
221     Returns:
222         output (Tensor[image_channels, image_height, image_width])
223     """
224     data = read_file(path)
225     return decode_image(data, mode)
226
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchvision/io/image.py b/torchvision/io/image.py
--- a/torchvision/io/image.py
+++ b/torchvision/io/image.py
@@ -61,7 +61,12 @@
     """
     Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
     Optionally converts the image to the desired format.
-    The values of the output tensor are uint8 between 0 and 255.
+    The values of the output tensor are uint8 in [0, 255], except for
+    16-bits pngs which are int32 tensors in [0, 65535].
+
+    .. warning::
+        Should pytorch ever support the uint16 dtype natively, the dtype of the
+        output for 16-bits pngs will be updated from int32 to uint16.
 
     Args:
         input (Tensor[1]): a one dimensional uint8 tensor containing
@@ -188,7 +193,8 @@
     operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
 
     Optionally converts the image to the desired format.
-    The values of the output tensor are uint8 between 0 and 255.
+    The values of the output tensor are uint8 in [0, 255], except for
+    16-bits pngs which are int32 tensors in [0, 65535].
 
     Args:
         input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the
@@ -209,7 +215,8 @@
     """
     Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.
     Optionally converts the image to the desired format.
-    The values of the output tensor are uint8 between 0 and 255.
+    The values of the output tensor are uint8 in [0, 255], except for
+    16-bits pngs which are int32 tensors in [0, 65535].
 
     Args:
         path (str): path of the JPEG or PNG image.

{"golden_diff": "diff --git a/torchvision/io/image.py b/torchvision/io/image.py\n--- a/torchvision/io/image.py\n+++ b/torchvision/io/image.py\n@@ -61,7 +61,12 @@\n     \"\"\"\n     Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.\n     Optionally converts the image to the desired format.\n-    The values of the output tensor are uint8 between 0 and 255.\n+    The values of the output tensor are uint8 in [0, 255], except for\n+    16-bits pngs which are int32 tensors in [0, 65535].\n+\n+    .. warning::\n+        Should pytorch ever support the uint16 dtype natively, the dtype of the\n+        output for 16-bits pngs will be updated from int32 to uint16.\n \n     Args:\n         input (Tensor[1]): a one dimensional uint8 tensor containing\n@@ -188,7 +193,8 @@\n     operation to decode the image into a 3 dimensional RGB or grayscale Tensor.\n \n     Optionally converts the image to the desired format.\n-    The values of the output tensor are uint8 between 0 and 255.\n+    The values of the output tensor are uint8 in [0, 255], except for\n+    16-bits pngs which are int32 tensors in [0, 65535].\n \n     Args:\n         input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the\n@@ -209,7 +215,8 @@\n     \"\"\"\n     Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.\n     Optionally converts the image to the desired format.\n-    The values of the output tensor are uint8 between 0 and 255.\n+    The values of the output tensor are uint8 in [0, 255], except for\n+    16-bits pngs which are int32 tensors in [0, 65535].\n \n     Args:\n         path (str): path of the JPEG or PNG image.\n", "issue": "Loading 16bit png images\nported here from https://github.com/pytorch/pytorch/issues/32971\r\n\r\nOriginal description:\r\n\r\nWhen I was trying  to load 16 bit .png grayscale image with torchvision.datasets.imagefolder ,it is loading every image as white only. \r\nI solved this issue by doing transformation operations outside Compose function.\r\n\r\n\r\ncc @pmeier @wanifarooq @choidongyeon \n", "before_files": [{"content": "from enum import Enum\n\nimport torch\n\nfrom .._internally_replaced_utils import _get_extension_path\n\n\ntry:\n    lib_path = _get_extension_path(\"image\")\n    torch.ops.load_library(lib_path)\nexcept (ImportError, OSError):\n    pass\n\n\nclass ImageReadMode(Enum):\n    \"\"\"\n    Support for various modes while reading images.\n\n    Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,\n    ``ImageReadMode.GRAY`` for converting to grayscale,\n    ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,\n    ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for\n    RGB with transparency.\n    \"\"\"\n\n    UNCHANGED = 0\n    GRAY = 1\n    GRAY_ALPHA = 2\n    RGB = 3\n    RGB_ALPHA = 4\n\n\ndef read_file(path: str) -> torch.Tensor:\n    \"\"\"\n    Reads and outputs the bytes contents of a file as a uint8 Tensor\n    with one dimension.\n\n    Args:\n        path (str): the path to the file to be read\n\n    Returns:\n        data (Tensor)\n    \"\"\"\n    data = torch.ops.image.read_file(path)\n    return data\n\n\ndef write_file(filename: str, data: torch.Tensor) -> None:\n    \"\"\"\n    Writes the contents of a uint8 tensor with one dimension to a\n    file.\n\n    Args:\n        filename (str): the path to the file to be written\n        data (Tensor): the contents to be written to the output file\n    \"\"\"\n    torch.ops.image.write_file(filename, data)\n\n\ndef decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:\n    \"\"\"\n    Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 between 0 and 255.\n\n    Args:\n        input (Tensor[1]): a one dimensional uint8 tensor containing\n            the raw bytes of the PNG image.\n        mode (ImageReadMode): the read mode used for optionally\n            converting the image. Default: ``ImageReadMode.UNCHANGED``.\n            See `ImageReadMode` class for more information on various\n            available modes.\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    output = torch.ops.image.decode_png(input, mode.value)\n    return output\n\n\ndef encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor:\n    \"\"\"\n    Takes an input tensor in CHW layout and returns a buffer with the contents\n    of its corresponding PNG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width]): int8 image tensor of\n            ``c`` channels, where ``c`` must 3 or 1.\n        compression_level (int): Compression factor for the resulting file, it must be a number\n            between 0 and 9. Default: 6\n\n    Returns:\n        Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the\n            PNG file.\n    \"\"\"\n    output = torch.ops.image.encode_png(input, compression_level)\n    return output\n\n\ndef write_png(input: torch.Tensor, filename: str, compression_level: int = 6):\n    \"\"\"\n    Takes an input tensor in CHW layout (or HW in the case of grayscale images)\n    and saves it in a PNG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width]): int8 image tensor of\n            ``c`` channels, where ``c`` must be 1 or 3.\n        filename (str): Path to save the image.\n        compression_level (int): Compression factor for the resulting file, it must be a number\n            between 0 and 9. Default: 6\n    \"\"\"\n    output = encode_png(input, compression_level)\n    write_file(filename, output)\n\n\ndef decode_jpeg(\n    input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = \"cpu\"\n) -> torch.Tensor:\n    \"\"\"\n    Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 between 0 and 255.\n\n    Args:\n        input (Tensor[1]): a one dimensional uint8 tensor containing\n            the raw bytes of the JPEG image. This tensor must be on CPU,\n            regardless of the ``device`` parameter.\n        mode (ImageReadMode): the read mode used for optionally\n            converting the image. Default: ``ImageReadMode.UNCHANGED``.\n            See ``ImageReadMode`` class for more information on various\n            available modes.\n        device (str or torch.device): The device on which the decoded image will\n            be stored. If a cuda device is specified, the image will be decoded\n            with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only\n            supported for CUDA version >= 10.1\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    device = torch.device(device)\n    if device.type == \"cuda\":\n        output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)\n    else:\n        output = torch.ops.image.decode_jpeg(input, mode.value)\n    return output\n\n\ndef encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor:\n    \"\"\"\n    Takes an input tensor in CHW layout and returns a buffer with the contents\n    of its corresponding JPEG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width])): int8 image tensor of\n            ``c`` channels, where ``c`` must be 1 or 3.\n        quality (int): Quality of the resulting JPEG file, it must be a number between\n            1 and 100. Default: 75\n\n    Returns:\n        output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the\n            JPEG file.\n    \"\"\"\n    if quality < 1 or quality > 100:\n        raise ValueError(\"Image quality should be a positive number \" \"between 1 and 100\")\n\n    output = torch.ops.image.encode_jpeg(input, quality)\n    return output\n\n\ndef write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):\n    \"\"\"\n    Takes an input tensor in CHW layout and saves it in a JPEG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``\n            channels, where ``c`` must be 1 or 3.\n        filename (str): Path to save the image.\n        quality (int): Quality of the resulting JPEG file, it must be a number\n            between 1 and 100. Default: 75\n    \"\"\"\n    output = encode_jpeg(input, quality)\n    write_file(filename, output)\n\n\ndef decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:\n    \"\"\"\n    Detects whether an image is a JPEG or PNG and performs the appropriate\n    operation to decode the image into a 3 dimensional RGB or grayscale Tensor.\n\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 between 0 and 255.\n\n    Args:\n        input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the\n            PNG or JPEG image.\n        mode (ImageReadMode): the read mode used for optionally converting the image.\n            Default: ``ImageReadMode.UNCHANGED``.\n            See ``ImageReadMode`` class for more information on various\n            available modes.\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    output = torch.ops.image.decode_image(input, mode.value)\n    return output\n\n\ndef read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:\n    \"\"\"\n    Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 between 0 and 255.\n\n    Args:\n        path (str): path of the JPEG or PNG image.\n        mode (ImageReadMode): the read mode used for optionally converting the image.\n            Default: ``ImageReadMode.UNCHANGED``.\n            See ``ImageReadMode`` class for more information on various\n            available modes.\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    data = read_file(path)\n    return decode_image(data, mode)\n", "path": "torchvision/io/image.py"}], "after_files": [{"content": "from enum import Enum\n\nimport torch\n\nfrom .._internally_replaced_utils import _get_extension_path\n\n\ntry:\n    lib_path = _get_extension_path(\"image\")\n    torch.ops.load_library(lib_path)\nexcept (ImportError, OSError):\n    pass\n\n\nclass ImageReadMode(Enum):\n    \"\"\"\n    Support for various modes while reading images.\n\n    Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,\n    ``ImageReadMode.GRAY`` for converting to grayscale,\n    ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,\n    ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for\n    RGB with transparency.\n    \"\"\"\n\n    UNCHANGED = 0\n    GRAY = 1\n    GRAY_ALPHA = 2\n    RGB = 3\n    RGB_ALPHA = 4\n\n\ndef read_file(path: str) -> torch.Tensor:\n    \"\"\"\n    Reads and outputs the bytes contents of a file as a uint8 Tensor\n    with one dimension.\n\n    Args:\n        path (str): the path to the file to be read\n\n    Returns:\n        data (Tensor)\n    \"\"\"\n    data = torch.ops.image.read_file(path)\n    return data\n\n\ndef write_file(filename: str, data: torch.Tensor) -> None:\n    \"\"\"\n    Writes the contents of a uint8 tensor with one dimension to a\n    file.\n\n    Args:\n        filename (str): the path to the file to be written\n        data (Tensor): the contents to be written to the output file\n    \"\"\"\n    torch.ops.image.write_file(filename, data)\n\n\ndef decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:\n    \"\"\"\n    Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 in [0, 255], except for\n    16-bits pngs which are int32 tensors in [0, 65535].\n\n    .. warning::\n        Should pytorch ever support the uint16 dtype natively, the dtype of the\n        output for 16-bits pngs will be updated from int32 to uint16.\n\n    Args:\n        input (Tensor[1]): a one dimensional uint8 tensor containing\n            the raw bytes of the PNG image.\n        mode (ImageReadMode): the read mode used for optionally\n            converting the image. Default: ``ImageReadMode.UNCHANGED``.\n            See `ImageReadMode` class for more information on various\n            available modes.\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    output = torch.ops.image.decode_png(input, mode.value)\n    return output\n\n\ndef encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor:\n    \"\"\"\n    Takes an input tensor in CHW layout and returns a buffer with the contents\n    of its corresponding PNG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width]): int8 image tensor of\n            ``c`` channels, where ``c`` must 3 or 1.\n        compression_level (int): Compression factor for the resulting file, it must be a number\n            between 0 and 9. Default: 6\n\n    Returns:\n        Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the\n            PNG file.\n    \"\"\"\n    output = torch.ops.image.encode_png(input, compression_level)\n    return output\n\n\ndef write_png(input: torch.Tensor, filename: str, compression_level: int = 6):\n    \"\"\"\n    Takes an input tensor in CHW layout (or HW in the case of grayscale images)\n    and saves it in a PNG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width]): int8 image tensor of\n            ``c`` channels, where ``c`` must be 1 or 3.\n        filename (str): Path to save the image.\n        compression_level (int): Compression factor for the resulting file, it must be a number\n            between 0 and 9. Default: 6\n    \"\"\"\n    output = encode_png(input, compression_level)\n    write_file(filename, output)\n\n\ndef decode_jpeg(\n    input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = \"cpu\"\n) -> torch.Tensor:\n    \"\"\"\n    Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 between 0 and 255.\n\n    Args:\n        input (Tensor[1]): a one dimensional uint8 tensor containing\n            the raw bytes of the JPEG image. This tensor must be on CPU,\n            regardless of the ``device`` parameter.\n        mode (ImageReadMode): the read mode used for optionally\n            converting the image. Default: ``ImageReadMode.UNCHANGED``.\n            See ``ImageReadMode`` class for more information on various\n            available modes.\n        device (str or torch.device): The device on which the decoded image will\n            be stored. If a cuda device is specified, the image will be decoded\n            with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only\n            supported for CUDA version >= 10.1\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    device = torch.device(device)\n    if device.type == \"cuda\":\n        output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)\n    else:\n        output = torch.ops.image.decode_jpeg(input, mode.value)\n    return output\n\n\ndef encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor:\n    \"\"\"\n    Takes an input tensor in CHW layout and returns a buffer with the contents\n    of its corresponding JPEG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width])): int8 image tensor of\n            ``c`` channels, where ``c`` must be 1 or 3.\n        quality (int): Quality of the resulting JPEG file, it must be a number between\n            1 and 100. Default: 75\n\n    Returns:\n        output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the\n            JPEG file.\n    \"\"\"\n    if quality < 1 or quality > 100:\n        raise ValueError(\"Image quality should be a positive number \" \"between 1 and 100\")\n\n    output = torch.ops.image.encode_jpeg(input, quality)\n    return output\n\n\ndef write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):\n    \"\"\"\n    Takes an input tensor in CHW layout and saves it in a JPEG file.\n\n    Args:\n        input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``\n            channels, where ``c`` must be 1 or 3.\n        filename (str): Path to save the image.\n        quality (int): Quality of the resulting JPEG file, it must be a number\n            between 1 and 100. Default: 75\n    \"\"\"\n    output = encode_jpeg(input, quality)\n    write_file(filename, output)\n\n\ndef decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:\n    \"\"\"\n    Detects whether an image is a JPEG or PNG and performs the appropriate\n    operation to decode the image into a 3 dimensional RGB or grayscale Tensor.\n\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 in [0, 255], except for\n    16-bits pngs which are int32 tensors in [0, 65535].\n\n    Args:\n        input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the\n            PNG or JPEG image.\n        mode (ImageReadMode): the read mode used for optionally converting the image.\n            Default: ``ImageReadMode.UNCHANGED``.\n            See ``ImageReadMode`` class for more information on various\n            available modes.\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    output = torch.ops.image.decode_image(input, mode.value)\n    return output\n\n\ndef read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:\n    \"\"\"\n    Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.\n    Optionally converts the image to the desired format.\n    The values of the output tensor are uint8 in [0, 255], except for\n    16-bits pngs which are int32 tensors in [0, 65535].\n\n    Args:\n        path (str): path of the JPEG or PNG image.\n        mode (ImageReadMode): the read mode used for optionally converting the image.\n            Default: ``ImageReadMode.UNCHANGED``.\n            See ``ImageReadMode`` class for more information on various\n            available modes.\n\n    Returns:\n        output (Tensor[image_channels, image_height, image_width])\n    \"\"\"\n    data = read_file(path)\n    return decode_image(data, mode)\n", "path": "torchvision/io/image.py"}]}