Created
March 28, 2020 09:38
-
-
Save quantumjim/2b6c18f0261c976a1d055547cac9213c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import time | |
dark = 10 | |
while True: | |
# get new this | |
left = {} | |
right = {} | |
total = {} | |
for pic in ['this','last']: | |
image = Image.open(pic+'.png') | |
left[pic] = 0 | |
right[pic] = 0 | |
N = image.size[0]*image.size[1] | |
for x in range(image.size[0]): | |
for y in range(image.size[1]): | |
b = max(image.getpixel((x,y))[0:3]) | |
if x<float(image.size[0])/2: | |
left[pic] += float(b)/(float(N)/2) | |
else: | |
right[pic] += float(b)/(float(N)/2) | |
total[pic] = (left[pic]+right[pic])/2 | |
if total['this']>dark and total['last']>dark: | |
if total['this']>=total['last']: | |
print('left motor forwards '+str(float(100*right['this'])/255)) | |
print('right motor forwards '+str(float(100*left['this'])/255)) | |
else: | |
speed = float(100*(total['last']-total['this']))/255 | |
print('left motor backwards '+ str(speed) ) | |
print('right motor backwards '+ str(speed) ) | |
image = Image.open('this.png') | |
image.save('last.png') | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment