camera - Pygame Documentation

pygame documentation | || Pygame Home || Help Contents || Reference Index || Camera || Cdrom || Color || Cursors || Display || Draw || Event || Examples || Font || Gfxdraw || Image || Joystick || Key || Locals || Mask || Midi || Mixer || Mouse || Movie || Music || Overlay || Pixelarray || Pygame || Rect || Scrap || Sndarray || Sprite || Surface || Surfarray || Tests || Time || Transform

pygame.camera

pygame module for camera use

pygame.camera.colorspace - Surface colorspace conversion | Surface colorspace conversion
pygame.camera.list_cameras - returns a list of available cameras | returns a list of available cameras
pygame.camera.Camera - load a camera | load a camera

Pygame currently supports only Linux and v4l2 cameras.

`EXPERIMENTAL!:` This api may change or disappear in later pygame releases. If you use this, your code will very likely break with the next pygame release.

The Bayer to `RGB` function is based on:

     Sonix SN9C101 based webcam basic I/F routines
     Copyright (C) 2004 Takafumi Mizuno
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions
     are met:
     1. Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
     2. Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
     THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     SUCH DAMAGE.

New in pygame `1.9.0`.

pygame.camera.colorspace

Surface colorspace conversion
`pygame.camera.colorspace(Surface, format, DestSurface = None): return Surface`

Allows for conversion from `"RGB"` to a destination colorspace of `"HSV"` or `"YUV"`. The source and destination surfaces must be the same size and pixel depth. This is useful for computer vision on devices with limited processing power. Capture as small of an image as possible, `transform.scale()` it even smaller, and then convert the colorspace to `YUV` or `HSV` before doing any processing on it.

pygame.camera.list_cameras

returns a list of available cameras
`pygame.camera.list_cameras(): return [cameras]`

Checks the computer for available cameras and returns a list of strings of camera names, ready to be fed into `pygame.camera.Camera`.

pygame.camera.Camera

load a camera
`pygame.camera.Camera(device, (width, height), format): return Camera`

Camera.start - opens, initializes, and starts capturing | opens, initializes, and starts capturing
Camera.stop - stops, uninitializes, and closes the camera | stops, uninitializes, and closes the camera
Camera.get_controls - gets current values of user controls | gets current values of user controls
Camera.set_controls - changes camera settings if supported by the camera | changes camera settings if supported by the camera
Camera.get_size - returns the dimensions of the images being recorded | returns the dimensions of the images being recorded
Camera.query_image - checks if a frame is ready | checks if a frame is ready
Camera.get_image - captures an image as a Surface | captures an image as a Surface
Camera.get_raw - returns an unmodified image as a string | returns an unmodified image as a string

Loads a v4l2 camera. The device is typically something like "/dev/video0". Default width and height are 640 by 480. Format is the desired colorspace of the output. This is useful for computer vision purposes. The default is `RGB`. The following are supported:

- RGB - Red, Green, Blue
- YUV - Luma, Blue Chrominance, Red Chrominance
- HSV - Hue, Saturation, Value

Camera.start

opens, initializes, and starts capturing
`Camera.start(): return None`

Opens the camera device, attempts to initialize it, and begins recording images to a buffer. The camera must be started before any of the below functions can be used.

Camera.stop

stops, uninitializes, and closes the camera
`Camera.stop(): return None`

Stops recording, uninitializes the camera, and closes it. Once a camera is stopped, the below functions cannot be used until it is started again.

Camera.get_controls

gets current values of user controls
`Camera.get_controls(): return (hflip = bool, vflip = bool, brightness)`

If the camera supports it, get_controls will return the current settings for horizontal and vertical image flip as bools and brightness as an int. If unsupported, it will return the default values of (0, 0, 0). Note that the return values here may be different than those returned by set_controls, though these are more likely to be correct.

Camera.set_controls

changes camera settings if supported by the camera
`Camera.set_controls(hflip = bool, vflip = bool, brightness): return (hflip = bool, vflip = bool, brightness)`

Allows you to change camera settings if the camera supports it. The return values will be the input values if the camera claims it succeeded or the values previously in use if not. Each argument is optional, and the desired one can be chosen by supplying the keyword, like hflip. Note that the actual settings being used by the camera may not be the same as those returned by set_controls.

Camera.get_size

returns the dimensions of the images being recorded
`Camera.get_size(): return (width, height)`

Returns the current dimensions of the images being captured by the camera. This will return the actual size, which may be different than the one specified during initialization if the camera did not support that size.

Camera.query_image

checks if a frame is ready
`Camera.query_image(): return bool`

If an image is ready to get, it returns true. Otherwise it returns false. Note that some webcams will always return False and will only queue a frame when called with a blocking function like get_image(). This is useful to separate the framerate of the game from that of the camera without having to use threading.

Camera.get_image

captures an image as a Surface
`Camera.get_image(Surface = None): return Surface`

Pulls an image off of the buffer as an `RGB` Surface. It can optionally reuse an existing Surface to save time. The bit depth of the surface is either 24bits or the same as the optionally supplied Surface.

Camera.get_raw

returns an unmodified image as a string
`Camera.get_raw(): return string`

Gets an image from a camera as a string in the native pixelformat of the camera. Useful for integration with other libraries.