The image to load

import cv2  as cv

src = cv.imread("C:/Users/POG/Pictures/Autumn is coming WallPack/Timon Studler Mod.jpg"# read image cv.namedWindow()"input image", cv.window_autosize)"input image", SRC)# Put the image in the window0) # If not, exit CV. DestroyAllWindows ()Copy the code

We have the body up here, and we’re going to use the image above to read the format type inside and we’re going to define a test function

def get_image_info(image):
	print(type(image))# Image typeprint(image.shape)# Height, width, number of channelsprint(image.size)# sizeprint(image.dtype)
Copy the code

Note here that Python is an interpreted language and runs line by line, so define the functions before you run the body. You can also define the main function by using

if __name__ =="__main__":
    main()
Copy the code

And put it at the end, and then the program will read it all and then proceed to the main function, so it doesn’t matter where you put the individual function. The result is:Type numpy. Ndarray: height 2880, length 5120, three channels; 44236830 = 288051203;

Image save

Add to main:

cv.imwrite("D:/result.png",src)
Copy the code

Video operation

Camera operation

The code for

def video_demo(a):
    capture = cv.VideoCapture(0) #0To turn on a camerawhile(True):
        ret,frame = capture.read()
        frame= cv.flip(frame,1# Mirror switch CV. Imshow ("video",frame)
        c = cv.waitKey(50)
        if (c==27) :# Esc = 27, press ESC to exit
            break

Copy the code

The running result is:Cap.read () : Returns a Boolean value (True/False). Return true if the frame was read correctly, false otherwise. You can check this return value to see if the video is over.

Play the video from the file

Same as capturing video from camera, just change camera index and video file name. Select the appropriate cv2.waitKey () time when the frame is displayed, if this value is too small the video will be very fast, if it is too large the video will be very slow (this can be used to display the video in slow motion). Under normal circumstances, 25 milliseconds will do. (Almost??)

def video_demo(a):
    capture = cv.VideoCapture(E:/ movie /[sunnymovie www.ygdy8.com]. Hurrah. Bd.720p. Chinese and English subtitles. MKV") # Keep it simple0Change to movie pathwhile(True):
        ret,frame = capture.read()
        frame= cv.flip(frame,1# Mirror switch CV. Imshow ("video",frame)
        c = cv.waitKey(50)
        if (c==27) :# Esc = 27, press ESC to exit
            break

Copy the code

Added read FPS, size and duration functions

def video_demo(a):# Call camera Capture= cv.VideoCapture(E:/ movie /[sunnymovie www.ygdy8.com]. Hurrah. Bd.720p. Chinese and English subtitles. MKV")
    while(True):
        ret,frame = capture.read()
        frame=cv.flip(frame,1# Read video FPS, FPS = the capture size. Get (CV. CAP_PROP_FPS) size = (. Capture the get (CV. CAP_PROP_FRAME_WIDTH), the capture, the get (CV. CAP_PROP_FRAME_HEIGHT)) print("fps: {}\nsize: {}".format(FPS,size)) # Video read duration (frames) total =int(capture.get(cv.CAP_PROP_FRAME_COUNT))
        print("[INFO] {} total frames in video".format(total))
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27) :# esc to 27
            break

Copy the code

Overall code:

import cv2  as cv
import  numpy as np
def main(a):
    src = cv.imread("C:/Users/POG/Pictures/Autumn is coming WallPack/Timon Studler Mod.jpg")
    cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
    cv.imshow("input image",src)
    video_demo()
    cv.waitKey(0)

    cv.destroyAllWindows()
    get_image_info(src)
    cv.imwrite("D:/result.png",src)

def get_image_info(image):
    print(type(image))
    print(image.shape)
    print(image.size)
    print(image.dtype)
    pixel_data = np.array(image)
    print(pixel_data)


def video_demo(a):# Call camera Capture= cv.VideoCapture(E:/ movie /[sunnymovie www.ygdy8.com]. Hurrah. Bd.720p. Chinese and English subtitles. MKV")
    while(True):
        ret,frame = capture.read()
        frame=cv.flip(frame,1# Read video FPS, FPS = the capture size. Get (CV. CAP_PROP_FPS) size = (. Capture the get (CV. CAP_PROP_FRAME_WIDTH), the capture, the get (CV. CAP_PROP_FRAME_HEIGHT)) print("fps: {}\nsize: {}".format(FPS,size)) # Video read duration (frames) total =int(capture.get(cv.CAP_PROP_FRAME_COUNT))
        print("[INFO] {} total frames in video".format(total))
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27) :# esc to 27
            break

if __name__ =="__main__":
    main()
Copy the code

Run the screenshot: