Image Display and Saving Operation Based on python opencv3

  • 2021-07-03 00:20:41
  • OfStack

Image display and save operation based on python-opencv3, the specific code is as follows:


import cv2 as cv
import numpy as np            # Import library 
print("-------------------------------")
image = cv.imread("D:/1.jpeg")        # Write image 
cv.imshow("image",image)               # Display 
cv.waitKey()                  # Wait 
cv.destroyAllWindows()             # Close all windows 
cv.imwrite("D:\\2.jpeg",image)         # Save an image 

First, import the cv2 and numpy function libraries, and the cv.imread() The imred () function is used to write 1 image, and the prototype of the imred () function Mat imread(const String& filename,int flags = IMREAD_COLOR);

Returns the Mat object. The first parameter is the absolute path of the file, but not all file objects are supported. It supports the following files:

l Windows bitmaps - *.bmp, *.dib (always supported)

l JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)

l JPEG 2000 files - *.jp2 (see the Notes section)

l Portable Network Graphics - *.png (see the Notes section)

l WebP - *.webp (see the Notes section)

l Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)

l Sun rasters - *.sr, *.ras (always supported)

l TIFF files - *.tiff, *.tif (see the Notes section)

l OpenEXR Image files - *.exr (see the Notes section)

l Radiance HDR - *.hdr, *.pic (always supported)

l Raster and Vector geospatial data supported by Gdal (see the Notes section)

It should be noted that the function does not rely on identifying suffix names, but on identifying the encoding of content.

In fact, the image formats commonly used in our life are recognizable, so there is basically no need to worry about this problem.

The second parameter can convert the original image to 1. This parameter is very important and should not be set easily. The default is IMREAD_LOAD_GDAL, that is, the gdal driver is used to load the image. The commonly used ones are as follows:

l IMREAD_UNCHANGED Loads the original image, otherwise it may be trimmed

l IMREAD_GRAYSCALE Load Single Channel Gray Image

l IMREAD_COLOR Load 3-channel BGR image

Others will not be used basically, so they will not be repeated here.

The following line cv. imshow () is the display image, its parameter list ("image name", image),

Cv. waitKey () is a wait function, without which you exit immediately without seeing the image,

Parameter list ("delay"): delay=0 (infinite wait), delay > 0 (wait for delayms), delay < 0 (Wait for any key to be clicked)

Cv. destroyAllWindows () is to close all windows after running the program, which is not necessary, but should be done for good programming habits.

Finally, cv. imwrite () is to save the image. Its parameter (path name, image name) is to save the image to the specified path (for example, "D:/Ambitio/demo. jpeg" is to save the image to the Ambitio folder of D with the name demo and the format jpeg)

Summarize


Related articles: