If you have any ideas or techniques you’d like to share, feel free to leave them in the comments section.

This article walks you through a method called imwrite.

OpenCV saves the image

After reading the image, the corresponding is to save the image, in OpenCV, save the image is also exquisite.

In C++, OpenCV functions are structured as follows:

CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
              const std::vector<int>& params = std::vector<int> ());
Copy the code
  • Parameter 1: const String& filename indicates the name of the file to be written. The suffix must be added, for exampletest.jpg;
  • Parameter 2: InputArray img indicates the image data of Mat type;
  • Argument 3: const STD ::vector& params represents the parameter encoding stored in a particular format. It has a default value STD ::vector< int >(), so it is not normally written.

The same applies in Python, where the syntax format is as follows.

# cv2.imwrite(save path, image matrix [, save id])
cv2.imwrite(filename, img[, params])
Copy the code
  • Parameter 1: save path and file name;
  • Parameter 2: image matrix;
  • Parameter 3: A format – specific save identifier. In the case of JPEG, this represents the quality of the image as an integer from 0 to 100. Default is 95. For PNG, the third parameter indicates the compression level.

The specific contents of the save label are as follows:

  1. cv2.CV_IMWRITE_JPEG_QUALITYSet image quality to.jpeg or.jpg with values ranging from 0 to 100 (the higher the value, the higher the quality). Default is 95.
  2. cv2.CV_IMWRITE_WEBP_QUALITYSet the image quality to. Webp format, and the value is 0 to 100.
  3. cv2.CV_IMWRITE_PNG_COMPRESSIONSets the compression ratio for the. PNG format from 0 to 9 (the higher the value, the higher the compression ratio). The default is 3.

Test a piece of code as follows, read the picture, save the picture.

import cv2
img = cv2.imread("maoyan.jpg".0)

# cv2.imshow("maoyan.png",img)
# imwrite Save image
cv2.imwrite("maoyan_grayscale.jpg", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

A grayscale image was successfully saved.

There is also a way to look up the properties of the above two graphs to see the property data on some of the images.

For JPG files, the third parameter in imWrite is to set the image quality. The value ranges from 1 to 100. The default value is 95.

cv2.imwrite("maoyan_3.jpg", img, (cv2.IMWRITE_JPEG_QUALITY, 5))
Copy the code

Sorting is that the higher the quality, the clearer the picture, the lower the quality, the blurred the picture, but the picture takes up less space.

The rest of the usage can continue to refer to the official manual.

When searching third-party information, I found that many bloggers wrote the following sentence.

Note that cv2.imwrite_jPEG_quality is of type Long and must be converted to int.Copy the code

The eraser actually tried it out and found it wasn’t Long. If any bloggers find it was something else, feel free to talk in the comments section. (And the long type has been removed in Python3)

The pit of imwrite

There are two things that need to be paid special attention to when learning imWrite. The first is the Chinese path problem, which exists in many programming scenarios.

Let’s start with reading. This is also a problem when reading.

import cv2
import numpy as np
# img = cv2.imread("./imgs/maoyan.jpg",-1)
img = cv2.imdecode(np.fromfile(". / images/maoyan. JPG",dtype=np.uint8),-1)

cv2.imshow("maoyan.png",img)
Copy the code

The second problem found during the test is that OpenCV will not automatically create a directory if the directory file does not exist.

# Save picture Chinese path problem
cv2.imencode('.jpg',img)[1].tofile("./ image _ directory does not exist /maoyan2.jpg")
Copy the code

End of today’s OpenCV

Another hour has passed. Have you mastered the basics of writing Python OpenCV images?

In your spare time, you can subscribe to the eraser crawler hundred example course to learn crawler knowledge.


Blogger ID: Dream eraser, hope you like, comment, collection.