opencv——python(1)

2023-01-31 01:01:29 python opencv
  1. 导入OpenCV模块

import cv2


2.导入numpy模块


import numpy as np


3.读取当前目录图片


img = cv2.imread("1.jpg")


4.创建图像


emptyImage = np.zeros(img.shape, np.uint8)


5.复制图像


emptyImage2 = img.copy()


6.得到图像副本,并做灰度处理


emptyImage3 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


7.显示图像


cv2.imshow("EmptyImage", emptyImage)
cv2.imshow("EmptyImage1", img)
cv2.imshow("EmptyImage2", emptyImage2)
cv2.imshow("EmptyImage3", emptyImage3)

8.写入保存图片


cv2.imwrite("./cat2.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 5])
cv2.imwrite("./cat3.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
cv2.imwrite("./cat.png", img, [int(cv2.IMWRITE_PNG_COMPRESSioN), 0])
cv2.imwrite("./cat2.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

9.窗口等待


cv2.waiTKEy(0)
cv2.destroyAllwindows()

相关文章