cv2
-
- cv2.imread()
- cv2.putText()
- cv2.imwrite()
- cv2.waitKey()
cv2.imread()
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)
- 输出:
cv2.putText()
# 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()
输出效果:
cv2.imwrite()
- 用于保存图片到指定文件夹下
- 如果数字为false代表保存失败;保存失败可能是文件夹不存在。
- 事例:
# 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)
cv2.waitKey()
- 一般都是跟着cv2.imshow()一起使用。 表示图片显示的时间长短,一般在图形界面上,显示图片需要进行设置显示图片时间长短。
- imshow后面不跟着waitkey图片只会在图形界面跳出一瞬间;
- waitkey中的参数值可以根据需要进行设置,一般都选择0或者不填写,不填写默认就是0.