Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ImageGrab
- import time
- import serial
- import ImageGrab
- from win32api import GetSystemMetrics
- #arduino
- # port = 'COM5'
- # serial = serial.Serial(port,9600,timeout=0)
- # time.sleep(2)
- ard = serial.Serial()
- def tryPort(com):
- port_name = "COM"+str(com)
- try:
- global ard
- ard = serial.Serial(port_name,9600,timeout=0)
- return True
- except:
- print(port_name+" unavailable")
- return False
- def init():
- available = False
- for comPort in range (1,12):
- if tryPort(comPort):
- available = True
- print ("Connection established at COM"+str(comPort)+", please wait.")
- time.sleep(2)
- break
- if available:
- #print ("Testing LEDs... Initializing RGB sequence")
- # flash()
- print ("Everything OK")
- select_region()
- def flash():
- ard.flush()
- ard.write("255,0,0X")
- time.sleep(0.5)
- ard.write("0,255,0X")
- time.sleep(0.5)
- ard.write("0,0,255X")
- time.sleep(0.5)
- ard.write("0,0,0X")
- active_region = 0
- def select_region():
- print("Choose screen region (top/middle/bottom)")
- global active_region
- region = raw_input(">")
- if region=="top": active_region = 500
- elif region=="middle": active_region = h/2
- elif region=="bottom": active_region = h-150
- else: print "No valid region selected, selected middle as default"
- print("Scanning pixels at height: "+str(h-active_region)+" px")
- #window
- w = GetSystemMetrics(0)
- h = GetSystemMetrics(1)
- x1 = w/2-w/3
- x2 = w/2+w/3
- def avg_color():
- image = ImageGrab.grab()
- # print 'width:',w,', height:',h
- r,g,b=0,0,0
- count = 0
- temp_col = "" # used for y-values
- y = active_region
- _range = range(w/2-1000, w/2+1000+1, 400)
- # print(_range)
- for k in _range:
- count+=2
- color = str(image.getpixel((k,y))).translate(None,'() ')
- rgb = color.split(',')
- r+=int(rgb[0])
- g+=int(rgb[1])
- b+=int(rgb[2])
- color = str(image.getpixel((k+200,int(y-y/3)))).translate(None,'() ')
- rgb = color.split(',')
- r+=int(rgb[0])
- g+=int(rgb[1])
- b+=int(rgb[2])
- # output: (r, g, b)->r,g,b
- r,g,b = r/count, g/count, b/count
- # g = int(g*0.75)
- # b = int(b*0.3)
- r = int(r*1.5)
- g= int(g*1.5)
- b = int(b*1.1)
- return str(r)+","+str(g)+","+str(b)+"X"
- def main():
- init()
- prev_col = None
- while (True):
- ard.flush()
- col = avg_color()
- if col!=prev_col:
- ard.write(col)
- prev_col = col
- time.sleep(0.45)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement