Pages

Wednesday, June 12, 2013

Pygame.display learning notes

Now I am expanding TestWebcam() to TestWebCam01().

def TestWebcam():

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

    webCamWidth  = 1920
    webCamHeight = 1024

    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 ***")


def TestWebcam01():

    # *** Program begin message ***

ftprint.PrintDoubleSpaceLine("*** Sample run begin - Test web cam 2013jun12hkt15:22 ***")    
       
    # *** Image/Display size ***

    # *** Vga ***

VgaSize = (640, 480)

# *** Logitech C920 ***

LogitechC920size = (1920, 1024)

    # *** initialize pygame.camera module ***

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

    # *** Create and start webcam object ***

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

  webCam.start()

    # *** Create a display  ***

    pygame.display.init()
    webCamDisplay = pygame.display.set_mode(VgaSize, 0)

    # *** Create a surface  ***

    webCamSurface = pygame.surface.Surface(VgaSize, 0, webCamDisplay)     
    
# *** Get image ***

    webCamSurface = webCam.get_image(webCamSurface)

time.sleep(0.1)

    # *** Save surface as a BMP file ***

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

    webCam.stop()
    pygame.quit()

    # *** blit surface to display, then flip ***

    webCamDisplay.blit(webCamSurface, (0,0))
    pygame.display.flip()

    # *** Print program end message ***

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

# .END

No comments:

Post a Comment