本程序实现经过笔记本摄像头拍照,而后在照片的右下角处加入时间戳的水印,分为2个函数分别实现拍照和加时间戳水印。用的时候本身先把须要的库都安装了,如下代码在Win七、Python3.6里调试经过__author__ = 'Yue Qingxuan'# -*- coding: utf-8 -*-import cv2import timeimport osfrom PIL import Image, ImageDraw, ImageFontdef TakePhoto(path): cap = cv2.VideoCapture(0) while(1): # get a frame ret, frame = cap.read() # show a frame cv2.imshow("Capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): #按字母q退出 timestr=time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) filename=path+'/{0}.jpeg'.format(timestr) #取当前时间做为照片的文件名 cv2.imwrite(filename, frame) add_watermark(filename) break cap.release() cv2.destroyAllWindows()def add_watermark(img_file): # 建立绘画对象 image = Image.open(img_file) draw = ImageDraw.Draw(image) myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf',size=20) fillcolor = '#ff0000' #RGB红色 timestr=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) #格式化时间 width,height = image.size # 参数一:位置(x轴,y轴);参数二:填写内容;参数三:字体;参数四:颜色 draw.text((width - 200, height-35), timestr, font=myfont, fill=fillcolor) image.save(img_file)if __name__ == '__main__': path="E:/OpencvVideo" if os.path.exists(path)==False: os.makedirs(path) #若是不存在就新建一个目录 TakePhoto(path)