cv2.imread()、cv2.putText、cv2.imwrite()、cv2.waitKey()

cv2.imread()

  • Used to read image data
  • Case demonstration:
import cv2
 # ouput img properties
img_path='C:/Users/WHY/Pictures/Saved Pictures/OIP-C (1).jfif'
def funOutputImgProperties(img):
    print("properties:shape:{},size:{},dtype:{}".format(img.shape,img.size,img.dtype))


# 3 channels img loads
# 读入完整图片,含alpha通道
img3ChaCom = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
cv2.imshow('IMREAD_UNCHANGED+Color',img3ChaCom)
cv2.waitKey()
funOutputImgProperties(img3ChaCom)

# 读入彩色图片,忽略alpha通道
img3Cha=cv2.imread(img_path,cv2.IMREAD_COLOR)
cv2.imshow('IMREAD_COLOR+Color', img3Cha)
cv2.waitKey()
funOutputImgProperties(img3Cha)

#彩色图片按,灰度图片读入
img3ChaGray=cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
cv2.imshow('IMREAD_GRAYSCALE+Color', img3ChaGray)
cv2.waitKey()
funOutputImgProperties(img3ChaGray)

  • Output:
  • Insert image description here
    Insert image description here

cv2.putText()

  • Used to draw text on an image
# Python program to explain cv2.putText() method

# importing cv2
import cv2

# path
path = r'C:\Users\WHY\Pictures\Saved Pictures\OIP-C (1).jfif'

# Reading an image in default mode
image = cv2.imread(path)

# Window name in which image is displayed
window_name = 'Image'

# font
font = cv2.FONT_HERSHEY_SIMPLEX

# org
org = (50, 50)

# fontScale
fontScale = 1
# Blue color in BGR
color = (255, 255, 255)
# Line thickness of 2 px
thickness = 2
# Using cv2.putText() method
image = cv2.putText(image, 'Marilyn Monroe', org, font,
                    fontScale, color, thickness, cv2.LINE_AA)
# Displaying the image
cv2.imshow(window_name, image)
cv2.waitKey()

Output effect:
Insert image description here

cv2.imwrite()

  • Used to save pictures to a specified folder
  • If the number is false, it means the save failed; the save failure may be that the folder does not exist.
  • Example:
# Python program to explain cv2.putText() method

# importing cv2
import cv2

# path
path = r'C:\Users\WHY\Pictures\Saved Pictures\OIP-C (1).jfif'

# Reading an image in default mode
image = cv2.imread(path)

# Window name in which image is displayed
window_name = 'Image'

# font
font = cv2.FONT_HERSHEY_SIMPLEX

# org
org = (50, 50)

# fontScale
fontScale = 1
# Blue color in BGR
color = (255, 255, 255)
# Line thickness of 2 px
thickness = 2
# Using cv2.putText() method
image = cv2.putText(image, 'Marilyn Monroe', org, font,
                    fontScale, color, thickness, cv2.LINE_AA)
# Displaying the image
cv2.imshow(window_name, image)
cv2.waitKey()
s=cv2.imwrite('./mm.jpg',image)
print(s)

  • Output result:

Insert image description here

cv2.waitKey()

  • It is generally used together with cv2.imshow(). Indicates the length of time the picture is displayed. Generally, on the graphical interface, displaying pictures requires setting the length of time the picture is displayed.
  • If the imshow is not followed by the waitkey image, it will only pop up in the graphical interface for a moment;
  • The parameter value in waitkey can be set as needed. Generally, 0 is selected or not filled in. If not filled in, the default value is 0.

おすすめ

転載: blog.csdn.net/qq_38978225/article/details/128718549