cv3.io.imwrite

cv3.io.imwrite(img_path, img, mkdir=False, ascii=True)[source]

Write an image to a file.

Parameters:
  • img_path (str or Path) – Path to the image file to write.

  • img (numpy.ndarray) – Image to write.

  • mkdir (bool, optional) – If True, create parent directories if they don’t exist. Defaults to False.

  • ascii (bool, optional) – If True, use ASCII filename handling. If False, use non-ASCII filename handling with numpy’s tofile method. Defaults to True.

Raises:

OSError – If the image cannot be written to the file.

Note

This function automatically handles RGB/BGR color space conversion based on the opt.RGB setting. When opt.RGB is True (default), the image is converted from RGB to BGR before writing.

When ascii=False, the function uses numpy’s tofile method to handle non-ASCII filenames, which is useful for international characters in filenames.

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[:, :] = [255, 0, 0]  # Blue image (BGR)
>>> # Write image to file
>>> cv3.imwrite('output.jpg', img)
>>> # Write image with automatic directory creation
>>> cv3.imwrite('output_dir/output.jpg', img, mkdir=True)