encode_image

lib.image.encode_image(image: ndarray, extension: str, encoding_args: tuple[int, ...] | None = None, metadata: PNGHeader | dict[str, Any] | bytes | None = None) bytes

Encode an image.

Parameters:
  • image (ndarray) – The image to be encoded in BGR channel order.

  • extension (str) – A compatible cv2 image file extension that the final image is to be saved to.

  • encoding_args (tuple[int, ...] | None) – Any encoding arguments to pass to cv2’s imencode function

  • metadata (PNGHeader | dict[str, Any] | bytes | None) – Metadata for the image. If provided, and the extension is png or tiff, this information will be written to the PNG itxt header. Default:None Can be provided as a python dict or pre-encoded

Returns:

encoded_image – The image encoded into the correct file format as bytes

Return type:

bytes

Example

>>> image_file = "/path/to/image.png"
>>> image = read_image(image_file)
>>> encoded_image = encode_image(image, ".jpg")