screen-monitor/src/screenmonitor.py
PAlexanderFranklin 75a7d0486c Fix comments
2024-08-02 13:55:19 -07:00

100 lines
3.2 KiB
Python

import os
import random
import time
from datetime import datetime, timedelta
from PIL import Image, ImageFilter
import pyautogui
# Define the directory where screenshots will be saved
output_dir = os.path.expanduser("~/.screenmonitor/output")
screenshotIntervalInMinutes = 17
# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)
def capture_screenshot():
# Capture screenshot
screenshot = pyautogui.screenshot()
# Apply blur
blurred_screenshot = screenshot.filter(ImageFilter.GaussianBlur(10))
# resize
width, height = blurred_screenshot.size
width, height = width // 5, height // 5
blurred_screenshot = blurred_screenshot.resize((width, height), Image.NEAREST)
# Randomly turn 10% of the pixels grey
pixels = blurred_screenshot.load()
total_pixels = width * height
black_pixels_count = int(total_pixels * 0.1)
neighbor_copy_pixels_count = int(total_pixels * 0.01)
for _ in range(black_pixels_count):
x = random.randint(0, width - 1)
y = random.randint(0, height - 1)
shade = random.randint(0, 255)
pixels[x, y] = (shade, shade, shade)
# Make 1% of the pixels copy themselves to their 8 neighbors
for _ in range(neighbor_copy_pixels_count):
x = random.randint(0, width - 1)
y = random.randint(0, height - 1)
original_color = pixels[x, y]
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if 0 <= x + dx < width and 0 <= y + dy < height:
pixels[x + dx, y + dy] = original_color
# Compress and save
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(output_dir, f"screenshot_{timestamp}.jpeg")
blurred_screenshot.save(filename, "JPEG", quality=20)
print(f"Screenshot saved to {filename}")
def delete_old_screenshots():
# Define the time threshold (72 hours ago)
time_threshold = datetime.now() - timedelta(hours=72)
for filename in os.listdir(output_dir):
if filename.startswith("screenshot"):
file_path = os.path.join(output_dir, filename)
file_mtime = datetime.fromtimestamp(os.path.getmtime(file_path))
if file_mtime < time_threshold:
os.remove(file_path)
print(f"Deleted old screenshot: {file_path}")
def main():
while True:
# Calculate the random time within the next 17-minute interval
next_interval_start = datetime.now() + timedelta(
seconds=screenshotIntervalInMinutes * 60
)
random_seconds = random.randint(0, screenshotIntervalInMinutes * 60)
random_time = datetime.now() + timedelta(seconds=random_seconds)
# Sleep until the random time within the interval
time_to_sleep = (random_time - datetime.now()).total_seconds()
if time_to_sleep > 0:
time.sleep(time_to_sleep)
# Capture the screenshot
capture_screenshot()
# Delete old screenshots
delete_old_screenshots()
# Sleep for the remainder of the 17-minute interval
remaining_interval = (next_interval_start - datetime.now()).total_seconds()
if remaining_interval > 0:
time.sleep(remaining_interval)
if __name__ == "__main__":
main()