[Python] 纯文本查看 复制代码 import time
from datetime import datetime, timedelta
import numpy as np
import requests # Import the requests library
from PIL import ImageGrab
def get_screen_region_screenshot(region):
"""
获取屏幕指定区域的截图
"""
screenshot = ImageGrab.grab(bbox=region)
return np.array(screenshot)
def send_http_request():
"""
发送HTTP请求的函数
"""
# 替换接收通知的Url,我这里使用的是iOS的Bark App
url = "https://xxxxxxxxxxxxxxxx"
payload = {"key": "value"}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
# 打印结果
print("HTTP Response:", response.text)
def send_http_request2():
"""
发送HTTP请求的函数
"""
# 替换接收通知的Url,我这里使用的是iOS的Bark App
url = "https://xxxxxxxxxxxxxxxx"
payload = {"key": "value"}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
# 打印结果
print("HTTP Response:", response.text)
def monitor_screen_region_changes(region, polling_interval=1, inactivity_duration=60):
"""
监控屏幕指定区域的像素变化
"""
previous_screenshot = get_screen_region_screenshot(region)
last_change_time = datetime.now()
while True:
current_screenshot = get_screen_region_screenshot(region)
if not np.array_equal(previous_screenshot, current_screenshot):
print("指定区域的像素发生变化")
# 发送HTTP请求
send_http_request()
# 更新前一次的截图和最后改变时间
previous_screenshot = current_screenshot
last_change_time = datetime.now()
elif datetime.now() - last_change_time >= timedelta(seconds=inactivity_duration):
print("内容1分钟未发生变化,请确认!")
# 发送HTTP请求
send_http_request2()
# 重置最后改变时间
last_change_time = datetime.now()
time.sleep(polling_interval)
if __name__ == "__main__":
# 替换为要监控的区域坐标 (left, top, right, bottom)
# left:左上角距离屏幕左侧的距离(像素)
# top:左上角距离屏幕上方的距离(像素)
# right:右下角距离屏幕左侧的距离(像素)
# bottom:右下角距离屏幕上方的距离(像素)
region_to_monitor = (595, 168, 1554, 806)
monitor_screen_region_changes(region_to_monitor)
老哥,我也是这样想的。我这里稍微修改了下:原理和楼主这个一样。只是加了个初始时间。实现:1分钟内无变化,发送"内容1分钟未发生变化,请确认!"。如果有变化,还是楼主的这个提醒。 |