惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

Python | 酷 壳 - CoolShell

Python修饰器的函数式编程 | 酷 壳 - CoolShell 函数式编程 | 酷 壳 - CoolShell 二叉树迭代器算法 | 酷 壳 - CoolShell 一些有意思的算法代码 | 酷 壳 - CoolShell Python 和 PyGame 的一些示例 | 酷 壳 - CoolShell 在Javascript里写Python | 酷 壳 - CoolShell 五大基于JVM的脚本语言 | 酷 壳 - CoolShell Python处理encoding的小技巧 | 酷 壳 - CoolShell Python程序员的进化 | 酷 壳 - CoolShell 非常简单的Python HTTP服务 | 酷 壳 - CoolShell Erlang和Python互通 | 酷 壳 - CoolShell Python也Spring了 | 酷 壳 - CoolShell Python 自然语言处理 | 酷 壳 - CoolShell 用TCC可以干些什么? | 酷 壳 - CoolShell
如何使用Python操作摄像头 | 酷 壳 - CoolShell
陈皓 · 2009-12-11 · via Python | 酷 壳 - CoolShell

用过USB摄像头的都知道,你需要使用鼠标来操作它,比如截个图,录个像什么的,要点N次鼠标,对于我们那些不喜欢多次点击鼠标的人来说,这是一件很boring的事情,所以,本文将教你如何使用Python来操作摄像头。

这里,我们需要三个Python库: VideoCapturePIL  和 pygame。使用这三个库你可以非常容易的编写一个摄像头程序。之所以使用pygame,其目的就是因为这个库可以处理视频帧(fps)。下面是代码:

from VideoCapture import Device
import ImageDraw, sys, pygame, time
from pygame.locals import *
from PIL import ImageEnhance

res = (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)

def 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.0
contrast = 1.0
shots = 0

while 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()

这段代码中的一些要点的解释如下:

  • 第15行的那个函数是在视频上显示些信息。这个例子中,显示的是抓图的数量以及当前的亮度和对比度。这个函数先显示深灰色的文本,然后偏移几个像素,再显示浅灰色的,这样可以有阴影的效果。
  • 第26行是在调整亮度和对比度。30-33行是在设置数字键1-4用于调整亮度和对比度。
  • 34 和35行是在设置 ‘q’ 和 ‘w’ 来显示摄像头的对话框。在那里你可以调整分辨率和暴光度等等。
  • 36行及以下的代码,是在存一个抓图文件。文件名中使用了当前时间。.

希望这个小程序能给你开启一个如何写摄像头的程序。

(全文完)

Loading...