码迷,mamicode.com
首页 > 编程语言 > 详细

Python 实现摄像头功能

时间:2014-06-16 23:10:24      阅读:568      评论:0      收藏:0      [点我收藏+]

标签:python   摄像头   pygame   

正确的程序如下所示:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

 

from VideoCapture import Device

import time

import sys,pygame

 

pygame.init()

 

size = width,height = 620,485

speed = [2,2]

black = 0,0,0

 

pygame.display.set_caption(‘视频窗口‘)

screen = pygame.display.set_mode(size)

 

#抓去频率,抓去一次

sleep_time_long = 0.1

 

#初始化摄像头

cam = Device(devnum=0,showVideoWindow=0)

 

while 1:

    #抓图

   cam.saveSnapshot(‘test.jpg‘,timestamp=3,boldfont=1,quality=75)

    #加载图像

   image = pygame.image.load(‘test.jpg‘)

    #传送画面

   screen.blit(image,speed)

    #显示图像

   pygame.display.flip()

    #休眠一下,等待一分钟

   time.sleep(sleep_time_long)

 

 

以下为笔者所写程序:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

 

from VideoCapture import DEVIDE

 

import time

import sys,pygame

 

[A1] 

size = width,height = 600,485

speed = [2,2[A2] ]

black[A3]  = (0,0,0)

 

pygame.display.set_Caption(‘视频窗口‘)

pygame[A4] .set_mode(size)

 

sleep_time_long=0.1

 

cam = DEVICE(devnum=0,VideoWindow=0)

 

while true:

   cam.saveSnapshot(‘test.jpg‘,timestamp=3,boldfont=1,quality=75)

   image = pygame.iamge.load(‘test.jpg‘)

   screen.blit[A5] ()

   pygame.display.flip()

time.sleep(sleep_time_long)

 

 

解释及延伸:

pygame.init

initializeall imported pygame modules
pygame.init():return (numpass, numfail)

Initialize all imported Pygamemodules. No exceptions will be raised if a module fails, but the total numberif successful and failed inits will be returned as a tuple. You can alwaysinitialize individual modules manually, but pygame.init is aconvenient way to get everything started. The init() functions forindividual modules will raise exceptions when they fail.

You may want to initalise thedifferent modules seperately to speed up your program or to not use things yourgame does not.

Itis safe to call this init() more than once: repeated calls willhave no effect. This is true even if you have pygame.quit –uninitializeall pygame modules– all the modules.

pygame.display.set_caption

set the current window caption
pygame.display.set_caption(title,icontitle=None): return None

If the display has a window title, thisfunction will change the name on the window. Some systems support an alternateshorter title to be used for minimized displays.

pygame.display.set_mode

initialize a window or screen for display
pygame.display.set_mode(resolution=(0,0),flags=0, depth=0): return Surface

This function will create a display Surface.The arguments passed in are requests for a display type. The actual createddisplay will be the best possible match supported by the system.

The resolution argument is a pair of numbersrepresenting the width and height. The flags argument is a collection ofadditional options. The depth argument represents the number of bits to use forcolor.

The Surface that gets returned can be drawnto like a regular Surface but changes will eventually be seen on the monitor.

If no resolution is passed or is set to (0,0) and pygame uses SDL version 1.2.10 or above, the created Surfacewill have the same size as the current screen resolution. If only the width orheight are set to 0, the Surface will have the same width or height as thescreen resolution. Using a SDL version prior to 1.2.10 will raise anexception.

It is usually best to not pass the depthargument. It will default to the best and fastest color depth for the system.If your game requires a specific color format you can control the depth withthis argument. Pygame will emulate an unavailable color depth which can beslow.

When requesting fullscreen display modes,sometimes an exact match for the requested resolution cannot be made. In thesesituations pygame will select the closest compatable match. The returnedsurface will still always match the requested resolution.

The flags argument controls which type ofdisplay you want. There are several to choose from, and you can even combinemultiple types using the bitwise or operator, (the pipe "|"character). If you pass 0 or no flags argument it will default to a softwaredriven window. Here are the display flags you will want to choose from:

  pygame.FULLSCREEN    create afullscreen display

  pygame.DOUBLEBUF     recommendedfor HWSURFACE or OPENGL

  pygame.HWSURFACE     hardwareaccelerated, only in FULLSCREEN

  pygame.OPENGL        create anopengl renderable display

  pygame.RESIZABLE     displaywindow should be sizeable

  pygame.NOFRAME       displaywindow will have no border or controls

surface和屏幕 
pygame 
最重要的部分就是surface。我们可以把surface看作是一张白纸。你可以对surface作很多操作,比如在surface上画线、用某种颜色 填充surface上的部分区域、把图片拷贝到surface上去,把图片从surface上复制下来、设置或者读取surface上某个点的颜色。一个 surface可以是任何大小,一个游戏可以有任意多surface。其中有一个surface是特别的,就是用 pygame.display.set_mode()创建的display surface。它代表了屏幕,对它的任何操作会出现在用户的屏幕上。一个游戏只能有一个这样的surface,这是SDL的限制。
 样创建surface?刚才提到,用pygame.display.set_mode()可以创建特殊的display surface。此外,还可以用image.load()创建一个包含图片的surface,还可以用font.render()创建一个包含文字的 surface。你甚至可以用Surface()创建一个不包含任何东西的surface
surface
的大部分方法都不重要,只要学习其中的blit(), fill(), set_at()get_at()就够用了。
display surface
的初始化操作是这样的:

   1 screen = pygame.display.set_mode((1024, 768))
 
或者
screen = pygame.display.set_mode((1024, 768), pygame.FULLSCREEN)
 
你可以用set_mode把原来窗口的游戏变成全屏。其它的俄显式模式(可以用|连接)有
DOUBLEBUF
 对于平滑的动画所必须 OPENGL 让你可以用PyOpenGL,但是不能用pygame的绘图函数 还有一个可选的depth参数,用来控制颜色显示的深度。一般情况下不用指定这个参数,只要用默认值就可以了。
如果使用DOUBLEBUF,你需要用flip函数来把绘制的内容显示到屏幕上。
>>> pygame.display.flip()
 
 
画图 
接下来,我们在屏幕上画一幅图像。我们通过最重要的画图原语BLIT(BLock Image Transfer)来实现,它可以把图像从一个地方(比如源图像)拷贝到另一个地方(比如屏幕上的某个位置)。
>>> car = pygame.image.load(‘car.png‘)>>> screen.blit(car, (50, 100))>>> pygame.display.flip()
 
这时,图片car.png中的内容会显示在屏幕上,图片的左上角在屏幕上的坐标是(50, 100)。屏幕坐标的X轴从作往右的,Y轴是从上往下的。然后我们可以用如下语句来旋转图片:
>>> rotated = pygame.transform.rotate(car, 90)>>> screen.blit(car, (50, 100))>>> pygame.display.flip()
 
要让屏幕上的任何东西动起来,需要一个这样的过程:画一个场景,然后把它擦掉,然后再画一幅稍微不同的场景,然后再擦掉……如此反复。比如:

   1 for i in range(100):   2     screen.fill((0, 0, 0))   3     screen.blit(rotated, (i*10, 0))   4     pygame.display.flip()
 
注意:这里我们把整个屏幕清楚然后重新绘制不是很好的方法。最好是只把屏幕上变化的部分擦掉重画,其它部分不变。Sprite模块可以帮助我们做这个事情。

 

pygame.image.load

load newimage from a file
pygame.image.load(filename):return Surface
pygame.image.load(fileobj,namehint=""): return Surface

Load an image from a file source. Youcan pass either a filename or a Python file-like object.

Pygame will automatically determinethe image type (e.g., GIF or bitmap)and create a new Surface object from the data. In some cases it will need toknow the file extension (e.g., GIF images shouldend in ".gif"). If you pass a raw file-like object, you may also wantto pass the original filename as the namehint argument.

The returnedSurface will contain the same color format, colorkey and alpha transparency asthe file it came from. You will often want to callSurface.convert –changethe pixel format of an image– with no arguments, to create a copythat will draw more quickly on the screen.

For alpha transparency, like in .pngimages use the convert_alpha() method afterloading so that the image has per pixel transparency.

Pygame may not always be built tosupport all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended –testif extended image formats can be loaded– returns‘True‘, you should be able to load most images (including png, jpg and gif).

You should use os.path.join() forcompatibility.

  eg. asurf = pygame.image.load(os.path.join(‘data‘, ‘bla.png‘))

pygame.display.flip

update the full display Surface to thescreen
pygame.display.flip():return None

This will update the contents of the entire display. Ifyour display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a verticalretrace and swap the surfaces. If you are using a different type of displaymode, it will simply update the entire contents of the surface.

When using an pygame.OPENGL display mode this will perform a gl buffer swap.

 


 [A1]需要初始化pygame.init()

 [A2]参数意思没搞明白

 [A3]Black没搞明白做啥用

 [A4]缺少screen

 [A5]缺少参数

Python 实现摄像头功能,布布扣,bubuko.com

Python 实现摄像头功能

标签:python   摄像头   pygame   

原文地址:http://blog.csdn.net/woody891/article/details/31361697

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!