Skip to content

Instantly share code, notes, and snippets.

@satetheus
Created May 19, 2019 23:17
Show Gist options
  • Save satetheus/35f4e943f6d31dafb37a915120a880d7 to your computer and use it in GitHub Desktop.
Save satetheus/35f4e943f6d31dafb37a915120a880d7 to your computer and use it in GitHub Desktop.
test of the detect_invader function
#! /usr/bin/env python
from pyautogui import screenshot
from time import time
def timer(f):
def wrapper():
start_time = time()
f()
elapsedTime = time() - start_time
print('In {} seconds.'.format(elapsedTime))
return wrapper
@timer
def invader_detect_original():
"""Takes a screen and looks for red clumps"""
image = screenshot()
red_count = 0
pink_count = 0
total = 0
for pixel in image.getdata():
total += 1
if pixel[0] > 160 and pixel[1] < 55 and pixel[2] < 60:
red_count += 1
elif pixel[0] > 150 and pixel[1] < 50 and pixel[2] > 150:
pink_count += 1
image.save('temp.png')
if red_count > 1000:
print('Red Invader detected')
return True
elif pink_count > 50:
print('Pink Invader detected')
return True
else:
return False
@timer
def invader_detect():
"""Takes a screenshot and looks for red clumps"""
image = screenshot(); image.save('temp.png')
red_count, pink_count = 0, 0
total = 0 # not used
for pixel in image.getdata():
total += 1 # not used
if pixel[0] > 100 and pixel[1] < 50:
if pixel[2] < 50: red_count += 1
elif pixel[2] > 150: pink_count += 1
if red_count > 1000:
print('Red Invader detected')
return True
elif pink_count > 25:
print('Pink Invader detected')
return True
return False
if __name__ == '__main__':
print('Original Function: ')
invader_detect_original()
print('New Function: ')
invader_detect()
@satetheus
Copy link
Author

satetheus commented May 20, 2019

Picture 1:
Pink: true
red: true

Original Function: 
In 0.9414188861846924 seconds.

New Function: 
Pink Invader detected
In 0.71964430809021 seconds.

Picture 2:
Pink: true
red: false

Original Function: 
In 1.3354735374450684 seconds.

New Function: 
Pink Invader detected
In 1.1821825504302979 seconds.

Picture 3:
Pink: false
red: false

Original Function: 
In 1.1582047939300537 seconds.

New Function: 
In 1.1176066398620605 seconds.

Picture 4:
Pink: true
Red: true

Original Function: 
Red Invader detected
In 0.8271162509918213 seconds.

New Function: 
Red Invader detected
In 0.4826817512512207 seconds.

Picture 5:
Pink: false
Red: false

Original Function: 
In 1.132232904434204 seconds.

New Function: 
In 1.094543218612671 seconds.

Picture 6:
Pink: false
Red: true

Original Function: 
In 1.2245235443115234 seconds.

New Function: 
Red Invader detected
In 0.9185280799865723 seconds.

Picture 7:
Pink: false
Red: false

Original Function: 
In 1.1624736785888672 seconds.

New Function: 
In 1.1337552070617676 seconds.

Picture 8:
Pink: false
Red: true

Original Function: 
Red Invader detected
In 1.0741603374481201 seconds.

New Function: 
Red Invader detected
In 0.7840070724487305 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment