1. 读取图像并显示
import cv2 img = cv2.imread('../samples/c/lena.jpg') cv2.nameWindow("Image") #可以不用此句 cv2.imshow("Image",img) cv2.waitKey(0) #否则一闪而过 cv2.destroyAllWindows() # 养成关闭的好习惯
2. 复制图像并保存
import cv2 import numpy as np img = cv2.imread("../opencv-2.4.9/samples/c/cat.jpg") emptyImage = np.zeros(img.shape,np.uint8) emptyImage2 = img.copy() emptyImage3 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow("EmptyImage",emptyImage) cv2.imshow("Image",img) cv2.imshow("emptyImage2",emptyImage2) cv2.imshow("emptyImage3",emptyImage3) 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]) cv2.waitKey() cv2.destroyAllWindows()
参考自: http://blog.csdn.net/sunny2038/article/details/9057415