python的一些功能代码
python打开摄像头
def testCamera(): cap = cv2.VideoCapture(0) while (cap.isOpened()): ret, image = cap.read() cv2.imshow("test", image) cv2.waitKey(1) cap.release()
ret为1,读取image成功,ret为-1,读取image失败。
opencv-python读入带有中文字符的图片路径,并保存该图片进入带有中文字符的路径。
def read_zhongwen_img(): imgpath='C:/Users/Administrator/Desktop/图片/messi.jpg' image=cv2.imdecode(numpy.fromfile(imgpath,dtype='uint8'),-1) print(image.shape) rootpath=imgpath.rsplit('/',1)[0] cv2.imencode('.bmp', image)[1].tofile(rootpath + '/1.jpg')
cv2.imdecode(buf,flags)
flags=-1,load image as it is.
cv2.imencode() 第一个参数,是编码的格式,返回的是tuple.返回值的第一个参数,是否编码成功。第二个参数是tuple的字符流。