#Markus Gritsch
#Condensed version by Ian Mallett
import vidcap
from PIL import Image, ImageFont, ImageDraw

class Device:
    def __init__(self, devnum=0, showVideoWindow=0):
        self.dev = vidcap.new_Dev(devnum, showVideoWindow)
    def displayCaptureFilterProperties(self):
        #Displays a dialog containing the property page of the capture filter.
        self.dev.displaycapturefilterproperties()
    def displayCapturePinProperties(self):
        #Displays a dialog containing the property page of the capture pin.
        self.dev.displaycapturepinproperties()
    def setResolution(self, width, height):
        self.dev.setresolution(width, height)
    def getBuffer(self):
        #Returns a string containing the raw pixel data.
        return self.dev.getbuffer()
    def getImage(self):
        buffer, width, height = self.getBuffer()
        if buffer:
            im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
            return im
    def saveSnapshot(self, filename, **keywords):
        """Saves a snapshot to the harddisk.

        The filetype depends on the filename extension.  Everything that PIL
        can handle can be specified (foo.jpg, foo.gif, foo.bmp, ...).

        filename:   String containing the name of the resulting file.

        timestamp:  see getImage()

        boldfont:   see getImage()

        textpos:    see getImage()

        Additional keyword arguments can be give which are just passed to the
        save() method of the Image class.  For example you can specify the
        compression level of a JPEG image by quality=75 (which is the default
        value anyway).

        """
        self.getImage().save(filename, **keywords)
