JpegEncoder#
- class torchcodec.encoders.JpegEncoder(img: Tensor)[source]#
Encoder for JPEG images.
For a tutorial, see: Encoding images.
Example
from torchcodec.encoders import JpegEncoder JpegEncoder(img).to_file("image.jpg") # or encode to a file-like object or to a tensor, see methods below.
- Parameters:
img (
torch.Tensor) – The image to encode, a 3-dimensional uint8 tensor in CHW layout with 1 (grayscale) or 3 (RGB) channels. If on a CUDA device, encoding is performed on the GPU with nvJPEG, and only 3-channel RGB is supported.
Examples using
JpegEncoder:- to_file(dest: str | Path, *, quality: int = 75) None[source]#
Encode the image into a JPEG file.
- Parameters:
dest (str or
pathlib.Path) – The path to the output file, e.g.image.jpg.quality (int, optional) – Quality of the output, between 1 and 100. Higher means better quality and larger file size. Default: 75.
- to_file_like(dest: RawIOBase | BufferedIOBase, *, quality: int = 75) None[source]#
Encode the image into a file-like object.
- Parameters:
dest – A writable file-like object supporting
writeandseek, such asio.BytesIO()or an open file in binary write mode.quality (int, optional) – Quality of the output, between 1 and 100. Higher means better quality and larger file size. Default: 75.
- to_tensor(*, quality: int = 75) Tensor[source]#
Encode the image into raw bytes, as a 1D uint8 tensor.
The returned tensor is on the same device as the input image (a CUDA input yields a CUDA tensor).
- Parameters:
quality (int, optional) – Quality of the output, between 1 and 100. Higher means better quality and larger file size. Default: 75.
- Returns:
The encoded bytes, a 1D uint8 tensor on the same device as the input image.
- Return type: