Saves an image to a specified file
cv.imwrite(filename, img)
cv.imwrite(filename, imgs)
cv.imwrite(filename, img, 'OptionName', optionValue, ...)
success = cv.imwrite(...)
Input
- filename Name of the file.
- img Image to be saved.
- imgs A cell-array of images, to be saved as one multi-page image
(currently only supported by Tiff encoder).
Output
- success optional output flag, true on success, false otherwise. If not
requested, the function throws an error on fail.
Options
- FlipChannels in case the input is color image, flips the color order
from MATLAB's RGB/RGBA to OpenCV's BGR/BGRA order. default true
The following format-specific save parameters are currently supported:
- JpegQuality For JPEG, it can be a quality from 0 to 100 (the higher is
the better). Default value is 95.
- JpegProgressive Enable JPEG features, 0 or 1, default is false.
- JpegOptimize Enable JPEG features, 0 or 1, default is false.
- JpegResetInterval JPEG restart interval, 0 - 65535, default is 0
(no restart).
- JpegLumaQuality Separate luma quality level, 0 - 100, default is 0
(don't use).
- JpegChromaQuality Separate chroma quality level, 0 - 100, default is 0
(don't use).
- PngCompression For PNG, it can be the compression level from 0 to 9.
A higher value means a smaller size and longer compression time. If
specified,
PngStrategy
is changed to Default
. Default value is 1
(best speed setting).
- PngStrategy For PNG; used to tune the compression algorithm. These
flags will be modify the way of PNG image compression and will be passed
to the underlying zlib processing stage. The strategy parameter only
affects the compression ratio but not the correctness of the compressed
output even if it is not set appropriately. One of:
- Default Use this value for normal data.
- Filtered Use this value for data produced by a filter (or predictor).
Filtered data consists mostly of small values with a somewhat random
distribution. In this case, the compression algorithm is tuned to
compress them better. The effect of
Filtered
is to force more Huffman
coding and less string matching; it is somewhat intermediate between
Default
and HuffmanOnly
.
- HuffmanOnly Use this value to force Huffman encoding only
(no string match).
- RLE (default) Use this value to limit match distances to one
(run-length encoding).
RLE
is designed to be almost as fast as
HuffmanOnly
, but give better compression for PNG image data.
- Fixed Using this value prevents the use of dynamic Huffman codes,
allowing for a simpler decoder for special applications.
- PngBilevel Binary level PNG, 0 or 1, controls packing of pixels per
bytes. If false, PNG files pack pixels of bit-depths 1, 2, and 4 into
bytes as small as possible. default is false.
- PxmBinary For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1,
to specify ASCII or binary encoding. default is true.
- ExrType override EXR storage type. Note that the EXR encoder only
accepts
single
images. One of:
- Half store as half precision (FP16), see cv.convertFp16
- Float store as single precision (FP32), this is the default.
- WebpQuality For WEBP, it can be a quality from 1 to 100 (the higher is
the better). By default (without any parameter) and for quality above 100
the lossless compression is used.
- PamTupleType For PAM, sets the TUPLETYPE field to the corresponding
string value that is defined for the format. One of:
- Null
- BlackWhite
- Grayscale
- GrayscaleAlpha
- RGB
- RGBA
For advanced uses, you can directly pass a vector of parameters:
- Params Format-specific save parameters encoded as pairs:
[paramId_1, paramValue_1, paramId_2, paramValue_2, ...]
.
The function cv.imwrite saves the image to the specified file. The image
format is chosen based on the filename extension (see cv.imread for the list
of extensions). Only 8-bit (or 16-bit unsigned uint16
in case of PNG,
JPEG 2000, and TIFF) single-channel or 3-channel (with RGB channel order)
images can be saved using this function. If the format, depth or channel
order is different, use cv.cvtColor to convert it before saving. Or, use the
universal cv.FileStorage I/O functions to save the image to XML or YAML
format.
(If the chosen encoder does not support the depth of the input image, the
image will be implicitly cast to 8-bit).
If the image cannot be saved (because of IO errors, improper permissions,
unsupported or invalid format), the function throws an error.
It is possible to store PNG images with an alpha channel using this function.
To do this, create 8-bit (or 16-bit) 4-channel image RGBA, where the alpha
channel goes last. Fully transparent pixels should have alpha set to 0,
fully opaque pixels should have alpha set to 255/65535.
Note: WEBP format can also store images with alpha channel.
See rgba_png_demo.m
sample for an example.