#!/usr/local/bin/python # # hello.light.45.py # # receive and display light level # # Neil Gershenfeld # CBA MIT 10/24/07 # # (c) Massachusetts Institute of Technology 2007 # Permission granted for experimental and personal use; # license for commercial sale available from MIT # # Modified by Inna Koyrakh # from Tkinter import * import serial import random import Image import ImageTk # # Filtering variables # WINDOW = 600 eps = 0.1 #saveflag = 0 # # Usable sensor values # filter = 0.0 minVal = 2000.0 maxVal = 1.0 R = 100 # # Open photo and extract its dimensions # imageFile = 'C:\\Documents and Settings\\Inna\\Desktop\\How to make (almost) anything\\Software\\photo2_small.jpg' im1 = Image.open(imageFile) im2 = im1 xdim, ydim = im1.size # # Canvas dimensions # cMin_x = 0 cMin_y = 0 cMax_x = 0.5*xdim cMax_y = 0.5*ydim # # Canvas center # center_x = 0.5*(cMin_x+cMax_x) center_y = 0.5*(cMin_y+cMax_y) # # open serial port # ser = serial.Serial('COM13',9600) ser.setDTR() def idle(parent,canvas): global minVal, maxVal, cMax_x, cMax_y, filter, eps, imageFile, im1, im2, im3 #saveflag # # idle routine # byte2 = 0 byte3 = 0 byte4 = 0 ser.flush() while 1: # # find framing # byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): break low = ord(ser.read()) high = ord(ser.read()) value = 256*high + low filter = (1-eps)*filter + eps*value # # learn current working range of sensor # if filter <= minVal: minVal = filter if filter >= maxVal: maxVal = filter # # GUI parameters # # set a max width and height # if (maxVal-minVal) > 0: rangeLight = int(round(maxVal-minVal)) if (maxVal-minVal) <= 0: rangeLight = 1 ratio = (filter-minVal)/rangeLight width = 1*xdim height = 1*ydim x = int(round(ratio*width)) y = int(round(ratio*height)) print x, y im2 = im1.resize((x,y), Image.NEAREST) im3 = ImageTk.PhotoImage(im2) canvas.create_image(0.5*cMax_x, 0.5*cMax_y, image=im3) canvas.update() parent.after_idle(idle,parent,canvas) # # set up GUI # # # Set up master # master = Tk() master.title('PhotoZoomer.py (q to exit)') master.bind('q','exit') # # Initialize canvas # canvas = Canvas(master, width=cMax_x, height=cMax_y, background='white') canvas.pack() # # start idle loop # master.after(100,idle,master,canvas) # ms allowed for startup master.mainloop()