Reading and writing of Opencv images

Table of contents

import cv2

read image data

create a window

waitKey method

close all windows

complete example

save Picture

example


import cv2

# 导入opencv包
import cv2

read image data

cv2.imread(path, flag)
Parameter Description:
path: The path to the image file to read.
flag (optional): Specifies how the image is read. It can be one of the following flags:
cv2.IMREAD_COLOR (default): Read the image as a color image, ignoring any transparency.
cv2.IMREAD_GRAYSCALE: Read the image as a grayscale image.
cv2.IMREAD_UNCHANGED: The image is read including an alpha channel (if present).
return value:
If the image was successfully read, the imread() function returns a NumPy array object representing the image data.
Returns None if the image could not be read (e.g. the file path is wrong or the file format is not supported).
    # 读取图像数据,保存到image变量里
    image = cv2.imread(path, cv2.IMREAD_COLOR)

 

create a window

# Create a new window that automatically resizes
cv2.namedWindow('new', cv2.WINDOW_AUTOSIZE)
cv2.imshow('new', 0)
# create a new window with resizable
cv2.namedWindow('new', cv2.WINDOW_NORMAL)
# Resize the window to the specified width and height (1920x1080 here)
cv2.resizeWindow('new', 1920, 1080)
# Create a display window named new and display image data
 cv2.imshow('new', image)

waitKey method

# Waiting for keyboard input, the waitKey method means waiting for a key, and will return the ascii value of the key
# 0 indicates any key press, other integers indicate the time to wait for the key press, in milliseconds, and the window will automatically close if no key operation occurs within the time.
# ord gets the ascii value of the character, and can judge the input character based on this, and then perform further operations
Key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
    cv2.destroyAllWindows()  
# 如果输入的是q  key & 0xFF将保留key的低八位(即最后8个二进制位),相当于对256取余。

close all windows

cv2.destroyAllWindows()

complete example

import cv2

# 展示图片
def show_image(path):
    # 读取图像数据,保存到image变量里
    image = cv2.imread(path, cv2.IMREAD_COLOR)
    # 检查图像是否成功读取
    if image is not None:
        # 创建一个自动调整大小的新窗口
        cv2.namedWindow('new', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('new', 0)

        # 创建一个具有可调整大小的新窗口
        cv2.namedWindow('new', cv2.WINDOW_NORMAL)
        # 将窗口大小调整为指定的宽度和高度(这里是1920x1080)
        cv2.resizeWindow('new', 1920, 1080)

        # 创建一个名为new的显示窗口,并显示图像数据
        cv2.imshow('new', image)

        # 等待键盘输入,waitKey方法表示等待按键, 会返回按键的ascii的值
        # 0表示任何按键, 其他整数表示等待按键的时间,单位是毫秒, 超过时间没有发生按键操作窗口会自动关闭.
        Key = cv2.waitKey(0)

        # ord获得字符的ascii的值,可以据此判断输入的字符,然后进行进一步操作
        # if key & 0xFF == ord('q'):  # 如果输入的是q  key & 0xFF将保留key的低八位(即最后8个二进制位),相当于对256取余。
        if Key:
            # 关闭所有窗口
            cv2.destroyAllWindows()
        print(f'{path}的图像成功读取')
        return 0
    else:
        print("无法读取图像")
        return -1

show_image('1.jpg')

save Picture

cv2.imwrite("path name", the image to be saved ( NumPy array object ))
img=cv2.imread('img.jpg')
cv2.imwrite("./123.png", img)

example

# 保存图片
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.resizeWindow('img', 320, 240)
img = cv2.imread("images/5.jpg")
# 利用while循环优化退出逻辑
print("开始保存图片,按esc键退出")
while True:
    cv2.imshow('img', img)
    key = cv2.waitKey(0)
    if key & 0xFF == 27:
        break
    elif key & 0xFF == ord('s'):
        # 使用imwrite保存图片
        cv2.imwrite("./123.png", img)
        print("图片保存成功")
    else:
        print(key)
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/m0_74921567/article/details/132384991