Skip to content

Instantly share code, notes, and snippets.

@DanBrink91
Created August 22, 2014 01:36
Show Gist options
  • Save DanBrink91/a1e8bee181c2e9ce4cb8 to your computer and use it in GitHub Desktop.
Save DanBrink91/a1e8bee181c2e9ce4cb8 to your computer and use it in GitHub Desktop.
Presses a key based on activity in UCF Student Union
import requests
import binascii
from PIL import Image, ImageFile
import subprocess
r = requests.get("http://webcams.sdes.ucf.edu/webcam/Stream/10", stream=True)
# Generate Image Object
def gen_image():
p = ImageFile.Parser()
start_write = False
for bytes in r.iter_content(2):
# Start of file
if binascii.hexlify(bytes) == 'ffd8':
start_write = True
if start_write:
p.feed(bytes)
# End of file
if binascii.hexlify(bytes) == 'ffd9':
return p.close()
def difference(im1, im2):
data1 = im1.getdata()
data2 = im2.getdata()
diff = 0
for i in range(len(data1)):
diff += abs(data1[i][0] - data2[i][0])
diff += abs(data1[i][1] - data2[i][1])
diff += abs(data1[i][2] - data2[i][2])
return diff
im = gen_image()
width, height = im.size
# Setup Crops
top_left = im.crop((0, 0, width/2, height/2))
top_right = im.crop((width/2, 0, width, height/2))
bot_left = im.crop((0, height/2, width/2, height))
bot_right = im.crop((width/2, height/2, width, height))
try:
# Baselines
base_tl = Image.open("top_left.jpg")
base_tr = Image.open("top_right.jpg")
base_bl = Image.open("bot_left.jpg")
base_br = Image.open("bot_right.jpg")
crop = ""
diff = 0
# check differences
top_left_diff = difference(top_left, base_tl)
if top_left_diff > diff:
diff = top_left_diff
crop = "top left"
top_right_diff = difference(top_right, base_tr)
if top_right_diff > diff:
diff = top_right_diff
crop = "top right"
bot_left_diff = difference(bot_left, base_bl)
if bot_left_diff > diff:
diff = bot_left_diff
crop = "bot left"
bot_right_diff = difference(bot_right, base_br)
if bot_right_diff > diff:
diff = bot_right_diff
crop = "bot right"
print crop
if crop == "top left":
subprocess.call(["xdotool", "key", "a"])
elif crop == "top right":
subprocess.call(["xdotool", "key", "b"])
elif crop == "bot left":
subprocess.call(["xdotool", "key", "Up"])
elif crop == "bot right":
subprocess.call(["xdotool", "key", "Down"])
except:
print "Run again, baseline images were created."
top_left.save("top_left.jpg")
top_right.save("top_right.jpg")
bot_left.save("bot_left.jpg")
bot_right.save("bot_right.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment