""" The **** KeeponDancing **** Python Capstone Project. CSSE 120 - Introduction to Software Development (Robotics). Winter term, 2011-2012. Team members: John Byers Dmitry Votintsev (all of them). The primary author of this module is: Dmitry Votintsev Most of the code in this project by that author appears in this module. Most of the functions in this module concern: -- camera interactions """ #The PIL library for python 3.2: #http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil from urllib.request import urlopen from PIL import Image import tkinter from tkinter import ttk import re import os def main(): print('hello') hardware = Camera() hardware.capturePicture() hardware.drawWindow() class Camera(): def __init__ (self): self.speed = None self.camera = None self.ip_number = 102 # Set for the camera YOU are using self.ip_query = '/snapshot.cgi?user=student&pwd=' self.ip = 'http://192.168.1.' + str(self.ip_number) + self.ip_query self.picLocation = 'C:/Blobs/' self.picName = 'camera' def cameraShowInterface(self): self.capturePicture() def setIP(self): """ Do not use, still under development """ for i in range (49): self.networkNum = 100 + i response = urlopen ('http://192.168.1.' + str(self.networkNum)) if re.search('IP Camera', response) != None: self.ip = 'http://192.168.1.' + str(self.networkNum) print (self.ip) break def capturePicture(self): """ Captures picture from the camera and saves it into jpg and gif. """ response = urlopen(self.ip) file = open(self.picLocation + self.picName + '.jpg', 'wb') file.write(response.read()) file.close() self.jpgToGif(self.picLocation + self.picName + '.jpg') def jpgToGif(self, image): img = Image.open(image) img.save(self.picLocation + self.picName + '.gif') #os.remove(image) def drawWindow(self): """ Displays the current image from the camera. Precondition: A tkinter.Tk() is running. """ root = tkinter.Tk() self.root = tkinter.Toplevel() self.mainFrame = ttk.Frame(self.root, height=600, width=800) gif = self.picLocation + self.picName + '.gif' photo = tkinter.PhotoImage(file=gif) self.cameraPhoto = ttk.Label(self.mainFrame, image=photo) self.cameraPhoto.image = photo self.cameraPhoto.grid() refreshButton = ttk.Button(self.mainFrame, text='Refresh!') refreshButton.grid() refreshButton['command'] = lambda: self.refreshImage() self.mainFrame.grid() root.mainloop() def refreshImage(self): """ Callback to take a new snapshot and refresh the image being displayed to show the new snapshot. """ self.capturePicture() gif = self.picLocation + self.picName + '.gif' photo = tkinter.PhotoImage(file=gif) self.cameraPhoto.configure(image=photo) self.cameraPhoto.image = photo if __name__ == '__main__': main()