@@ -32,22 +32,69 @@ def _encode_to_tensor_through_bytesio(img, param, to_file_like) -> Tensor:
3232
3333
3434class JpegEncoder :
35+ """Encoder for JPEG images.
36+
37+ Example:
38+
39+ .. code-block:: python
40+
41+ from torchcodec.encoders import JpegEncoder
42+
43+ JpegEncoder(img).to_file("image.jpg")
44+ # or encode to a file-like object or to a tensor, see methods below.
45+
46+ Args:
47+ img (``torch.Tensor``): The image to encode, a 3-dimensional uint8 tensor
48+ in CHW layout with 1 (grayscale) or 3 (RGB) channels. If on a CUDA
49+ device, encoding is performed on the GPU with nvJPEG, and only
50+ 3-channel RGB is supported.
51+ """
52+
3553 def __init__ (self , img : Tensor ) -> None :
3654 self ._img = img
3755
3856 def to_file (self , dest : str | Path , * , quality : int = 75 ) -> None :
57+ """Encode the image into a JPEG file.
58+
59+ Args:
60+ dest (str or ``pathlib.Path``): The path to the output file, e.g.
61+ ``image.jpg``.
62+ quality (int, optional): Quality of the output, between 1 and 100.
63+ Higher means better quality and larger file size. Default: 75.
64+ """
3965 self ._validate_quality (quality )
4066 _encode_jpeg_to_file (self ._img , str (dest ), quality )
4167
4268 def to_file_like (
4369 self , dest : io .RawIOBase | io .BufferedIOBase , * , quality : int = 75
4470 ) -> None :
71+ """Encode the image into a file-like object.
72+
73+ Args:
74+ dest: A writable file-like object supporting ``write`` and ``seek``,
75+ such as ``io.BytesIO()`` or an open file in binary write mode.
76+ quality (int, optional): Quality of the output, between 1 and 100.
77+ Higher means better quality and larger file size. Default: 75.
78+ """
4579 self ._validate_quality (quality )
4680 _encode_jpeg_to_file_like (
4781 self ._img , create_file_like_context (dest , True ), quality
4882 )
4983
5084 def to_tensor (self , * , quality : int = 75 ) -> Tensor :
85+ """Encode the image into raw bytes, as a 1D uint8 tensor.
86+
87+ The returned tensor is on the same device as the input image (a CUDA
88+ input yields a CUDA tensor).
89+
90+ Args:
91+ quality (int, optional): Quality of the output, between 1 and 100.
92+ Higher means better quality and larger file size. Default: 75.
93+
94+ Returns:
95+ torch.Tensor: The encoded bytes, a 1D uint8 tensor on the same
96+ device as the input image.
97+ """
5198 self ._validate_quality (quality )
5299 if self ._img .is_cuda :
53100 return _encode_jpeg_to_tensor_cuda (self ._img , quality )
@@ -65,20 +112,64 @@ def _validate_quality(quality: int) -> None:
65112
66113
67114class PngEncoder :
115+ """Encoder for PNG images.
116+
117+ Example:
118+
119+ .. code-block:: python
120+
121+ from torchcodec.encoders import PngEncoder
122+
123+ PngEncoder(img).to_file("image.png")
124+ # or encode to a file-like object or to a tensor, see methods below.
125+
126+ Args:
127+ img (``torch.Tensor``): The image to encode, a 3-dimensional uint8 tensor
128+ in CHW layout with 1 (grayscale) or 3 (RGB) channels.
129+ """
130+
68131 def __init__ (self , img : Tensor ) -> None :
69132 self ._img = img
70133
71134 def to_file (self , dest : str | Path , * , compression_level : int = 6 ) -> None :
135+ """Encode the image into a PNG file.
136+
137+ Args:
138+ dest (str or ``pathlib.Path``): The path to the output file, e.g.
139+ ``image.png``.
140+ compression_level (int, optional): zlib compression level between 0
141+ (no compression, fastest) and 9 (max compression, slowest).
142+ Default: 6.
143+ """
72144 _encode_png_to_file (self ._img , str (dest ), compression_level )
73145
74146 def to_file_like (
75147 self , dest : io .RawIOBase | io .BufferedIOBase , * , compression_level : int = 6
76148 ) -> None :
149+ """Encode the image into a file-like object.
150+
151+ Args:
152+ dest: A writable file-like object supporting ``write`` and ``seek``,
153+ such as ``io.BytesIO()`` or an open file in binary write mode.
154+ compression_level (int, optional): zlib compression level between 0
155+ (no compression, fastest) and 9 (max compression, slowest).
156+ Default: 6.
157+ """
77158 _encode_png_to_file_like (
78159 self ._img , create_file_like_context (dest , True ), compression_level
79160 )
80161
81162 def to_tensor (self , * , compression_level : int = 6 ) -> Tensor :
163+ """Encode the image into raw bytes, as a 1D uint8 tensor.
164+
165+ Args:
166+ compression_level (int, optional): zlib compression level between 0
167+ (no compression, fastest) and 9 (max compression, slowest).
168+ Default: 6.
169+
170+ Returns:
171+ torch.Tensor: The encoded bytes, a 1D uint8 tensor.
172+ """
82173 return _encode_to_tensor_through_bytesio (
83174 self ._img , compression_level , _encode_png_to_file_like
84175 )
0 commit comments