mt.imageio.imwrite

Extra imwrite functions.

Functions

  • imwrite_asyn(): An asyn function that saves an image file using imageio.v3.imwrite().

  • immencode(): Encodes a mt.cv.opencv.Image instance as a PNG or WEBP image with metadata.

  • immwrite_asyn(): An asyn function that saves an image with metadata to file.

  • immwrite(): Saves an image with metadata to file.

async mt.imageio.imwrite.imwrite_asyn(fname: str, image: ndarray, plugin: str | None = None, extension: str | None = None, format_hint: str | None = None, plugin_kwargs: dict = {}, context_vars: dict = {}, **kwargs)

An asyn function that saves an image file using imageio.v3.imwrite().

Parameters:
  • fname (str) – local filepath where the image will be saved. If “<bytes>” is provided, the function returns bytes instead of writes to a file.

  • image (numpy.ndarray) – the image to write to, in A, RGB or RGBA pixel format

  • plugin (str, optional) – The plugin to use. Passed directly to imageio’s imwrite function.

  • extension (str, optional) – File extension. Passed directly to imageio’s imwrite function.

  • format_hint (str, optional) – A format hint to help optimise plugin selection. Passed directly to imageio’s imwrite function.

  • plugin_kwargs (dict) – Additional keyword arguments to be passed to the plugin write call.

  • context_vars (dict) – a dictionary of context variables within which the function runs. It must include context_vars[‘async’] to tell whether to invoke the function asynchronously or not.

Returns:

If “<bytes>” is provided for argument fname, a bytes object is returned. Otherwise, it returns whatever mt.base.aio.write_binary() returns.

Return type:

int or bytes

See also

imageio.v3.imwrite

The underlying function for all the hard work.

mt.imageio.imwrite.immencode(imm: Image, encoding_format: str = 'png', lossless: bool = True, quality: int | None = None) bytes

Encodes a mt.cv.opencv.Image instance as a PNG or WEBP image with metadata.

Parameters:
  • imm (cv.Image) – an image with metadata

  • encoding_format ({'png', 'webp'}) – the encoding format

  • lossless (bool) – whether or not to compress with lossless mode. Only valid for ‘webp’ format. For ‘png’ format, it is always lossless.

  • quality (int, optional) – a number between 0 and 100. Only valid for ‘webp’. If not provided, 90 for lossless and 80 for lossy.

Returns:

data – the encoded image, ready for writing to file

Return type:

bytes

Notes

All metadata values are converted to json strings if they are not strings.

See also

imageio.v3.imwrite

the underlying imwrite function

async mt.imageio.imwrite.immwrite_asyn(filepath: str, imm: Image, file_format: str | None = None, file_mode: int = 436, file_write_delayed: bool = False, lossless: bool = True, quality: int | None = None, context_vars: dict = {}, logger=None)

An asyn function that saves an image with metadata to file.

Parameters:
  • filepath (str) – local filepath to save the content to.

  • imm (Image) – an image with metadata

  • file_format ({'hdf5', 'png', 'webp'}, optional) – format to be used for saving the content. If not provided, it will be figured out from the file extension.

  • file_mode (int) – file mode to be set to using os.chmod(). If None is given, no setting of file mode will happen.

  • file_write_delayed (bool) – Only valid in asynchronous mode and the format is ‘json’. If True, wraps the file write task into a future and returns the future. In all other cases, proceeds as usual.

  • lossless (bool) – whether or not to compress with lossless mode. Only valid for ‘webp’ format. For ‘png’ format, it is always lossless.

  • quality (int, optional) – a number between 0 and 100. Only valid for ‘webp’. If not provided, 90 for lossless and 80 for lossy.

  • context_vars (dict) – a dictionary of context variables within which the function runs. It must include context_vars[‘async’] to tell whether to invoke the function asynchronously or not.

  • logger (logging.Logger, optional) – logger for debugging purposes

Returns:

the number of bytes written to file

Return type:

int

mt.imageio.imwrite.immwrite(filepath: str, imm: Image, file_format: str | None = None, file_mode: int = 436, file_write_delayed: bool = False, lossless: bool = True, quality: int | None = None, logger=None)

Saves an image with metadata to file.

Parameters:
  • filepath (str) – local filepath to save the content to.

  • imm (Image) – an image with metadata

  • file_format ({'hdf5', 'png', 'webp'}, optional) – format to be used for saving the content. If not provided, it will be figured out from the file extension.

  • file_mode (int) – file mode to be set to using os.chmod(). If None is given, no setting of file mode will happen.

  • file_write_delayed (bool) – Only valid in asynchronous mode and the format is ‘json’. If True, wraps the file write task into a future and returns the future. In all other cases, proceeds as usual.

  • lossless (bool) – whether or not to compress with lossless mode. Only valid for ‘webp’ format. For ‘png’ format, it is always lossless.

  • quality (int, optional) – a number between 0 and 100. Only valid for ‘webp’. If not provided, 90 for lossless and 80 for lossy.

  • logger (logging.Logger, optional) – logger for debugging purposes

Returns:

the number of bytes written to file

Return type:

int