Python调用摄像头摄像并保存图片的方法源码,,用python语言来调用
Python调用摄像头摄像并保存图片的方法源码,,用python语言来调用
用python语言来调用摄像头,并自动拍摄图片并保存到指定位置的方法源码是怎么样的呢?
操作USB摄像头一定需要用到鼠标来操作。像是截图、录像都可以完成。如今学习了python计算机编程语言那就要动手来完成一件有成就感的事情吧,就像本文中教的那样,看看是如何使用Python来操作摄像头。
首先完成这件事情,需要用到Python库,有这三个: VideoCapture、PIL 和 pygame。
pygame,这个库可以处理视频帧(fps)。
下面关Python调用摄像头摄像并保存图片的方法源码中有几行是需要注意的:
15行,那个函数是在视频上显示些信息。这个例子中,显示的是抓图的数量以及当前的亮度和对比度。这个函数先显示深灰色的文本,然后偏移几个像素,再显示浅灰色的,这样可以有阴影的效果。
26行,是在调整亮度和对比度。
30-33行,是在设置数字键1-4用于调整亮度和对比度
34-35行,是在设置和来显示摄像头的对话框。在那里你可以调整分辨率和暴光度等等。
36行及以下,是在存一个抓图文件。文件名中使用了代码编写时的时间。.
#! /usr/bin/env python#coding=utf-8from VideoCapture import Deviceimport ImageDraw, sys, pygame, timefrom pygame.locals import *from PIL import ImageEnhanceres = (640,480)pygame.init()cam = Device()cam.setResolution(res[0],res[1])screen = pygame.display.set_mode((640,480))pygame.display.set_caption('Webcam')pygame.font.init()font = pygame.font.SysFont("Courier",11)#www.iplaypy.comdef disp(phrase,loc): s = font.render(phrase, True, (200,200,200)) sh = font.render(phrase, True, (50,50,50)) screen.blit(sh, (loc[0]+1,loc[1]+1)) screen.blit(s, loc)brightness = 1.0contrast = 1.0shots = 0while 1: camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness) camshot = ImageEnhance.Contrast(camshot).enhance(contrast) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() keyinput = pygame.key.get_pressed() if keyinput[K_1]: brightness -= .1 if keyinput[K_2]: brightness += .1 if keyinput[K_3]: contrast -= .1 if keyinput[K_4]: contrast += .1 if keyinput[K_q]: cam.displayCapturePinProperties() if keyinput[K_w]: cam.displayCaptureFilterProperties() #以下的代码,是在存一个抓图文件。文件名中使用了当前时间 if keyinput[K_s]: filename = str(time.time()) + ".jpg" cam.saveSnapshot(filename, quality=80, timestamp=0) shots += 1 camshot = pygame.image.frombuffer(camshot.tostring(), res, "RGB") screen.blit(camshot, (0,0)) disp("S:" + str(shots), (10,4)) disp("B:" + str(brightness), (10,16)) disp("C:" + str(contrast), (10,28)) pygame.display.flip()
编橙之家文章,
相关内容
- Python 类快速排序方法找到第定n小数的方法,python小数
- Python实现SYSLOG服务器与MYSQL数据库写入源码,pythonsysl
- python方法判断IP的合法性源码分享,python合法性,用pyt
- Python代码音乐mp3下载器源码分享,python下载器,用pytho
- python方法实现磁盘剩余空间查询操作,python磁盘,想知道
- python转换工具之汉语拼音与国际音标的实现,python转换
- Python方法完成自动合并文本文件操作,python文本文件
- Python math方法算24点代码详解,pythonmath,计算24点的方法
- 如何让Cookie.py模块用冒号做key,cookie.py冒号,python的Co
- Python制作 百度贴吧小爬虫.V0.1,python.v0.1,Python制作 百度
评论关闭