forked from KPR/KPRSpaceAPI
v1
This commit is contained in:
parent
c1c802aa77
commit
7ab5308949
35
spacer.py
Normal file
35
spacer.py
Normal file
@ -0,0 +1,35 @@
|
||||
import os
|
||||
from PIL import Image
|
||||
import glob
|
||||
|
||||
def get_latest_screenshot(directory):
|
||||
""" Get the latest screenshot file from the given directory. """
|
||||
list_of_files = glob.glob(os.path.join(directory, '*.jpg')) # You can change the file extension if needed
|
||||
latest_file = max(list_of_files, key=os.path.getctime)
|
||||
return latest_file
|
||||
|
||||
def process_image(image_path):
|
||||
""" Process the image to decide if it's dark or light. """
|
||||
with Image.open(image_path) as img:
|
||||
# Resize image to 2x2
|
||||
img = img.resize((2, 2))
|
||||
|
||||
# Get average color of the pixels
|
||||
average_color = sum(img.getdata(), (0, 0, 0))
|
||||
average_color = [color / 4 for color in average_color] # Since the image is 2x2
|
||||
|
||||
# Determine if the image is dark or light
|
||||
if sum(average_color) / 3 < 128: # Average RGB values less than 128 is considered dark
|
||||
return 'closed'
|
||||
else:
|
||||
return 'open'
|
||||
|
||||
# Directory where the screenshots are stored
|
||||
screenshot_directory = '/var/lib/motion/'
|
||||
|
||||
# Get the latest screenshot
|
||||
latest_screenshot = get_latest_screenshot(screenshot_directory)
|
||||
|
||||
# Process the image and determine hackspace status
|
||||
hackspace_status = process_image(latest_screenshot)
|
||||
print(f"The hackspace is {hackspace_status}.")
|
||||
Loading…
Reference in New Issue
Block a user