Pages

Tuesday, June 11, 2013

Saving a web cam captured surface to a bmp file






























Now that I have learned what is a surface, I tried to save the surface to a BMP file.  I expected that it might take me hours to do this thing.  But surprisingly it took me only a couple of minutes.  The only bug I made was because I did not know how to specify a name in linux.

And the 2 relevant statements are:

    imageSurface = webCam.get_image()

    pygame.image.save(imageSurface, "/home/pi/fongtoy/testImage.bmp")

lunch time.

.END


# ftwebcam v0.3 tlfong01 2013jun11

# *****************************************************************************
# Module - ftwebcam.py
# Description - Test Logitech webcam C920 and C270
# Reference
#   Pygame.camera Reference - http://www.pygame.org/docs/ref/camera.html
#   Pygame.camera Turorial -  http://www.pygame.org/docs/tut/camera/CameraIntro.html
# Notes
#   Image zise 2MP 16:9 = 1920 * 1080 = 2,073,600
#   /etc/motion/motion.conf setting
#     Daemon = off
#     webcam_localhost = on
#   /etc/default/motion setting
#     start_motion_daemon = no
# *****************************************************************************

# *****************************************************************************
# Imports
# *****************************************************************************

import time

import pygame
import pygame.camera
from pygame.locals import *

import ftprint

# *****************************************************************************
# Function - TestWebcam()
# Description -
#   Test Webcam
# *****************************************************************************

def TestWebcam():

    ftprint.PrintDoubleSpaceLine("*** Sample run begin - Test web cam 2013jun11hkt11:15 ***")  
   
    webCamWidth  = 640
    webCamHeight = 480

    pygame.init()
    pygame.camera.init()

    webCamList = pygame.camera.list_cameras()
    if webCamList:
        print "List of available cameras = ", webCamList
        webCam = pygame.camera.Camera(webCamList[0],(webCamWidth, webCamHeight))

    webCam.start()

    imageSurface = webCam.get_image()

    pygame.image.save(imageSurface, "/home/pi/fongtoy/testImage.bmp")

    webCam.stop()
    pygame.quit()

    ftprint.PrintDoubleSpaceLine("*** Sample run end ***")

# .END

.END





No comments:

Post a Comment