监控屏幕内容实时变化如何监控?
监控一个界面的内容变化,以确保自己能第一时间留意到
原理:实时获取界面像素,只要像素变化,代表界面有变化。
jiankong桌面.py
from PIL import ImageGrab
import numpy as np
import time
import requests # Import the requests library
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 monitor_screen_region_changes(region, polling_interval=1):
"""
监控屏幕指定区域的像素变化
"""
previous_screenshot = get_screen_region_screenshot(region)
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
time.sleep(polling_interval)
if __name__ == "__main__":
# 替换为要监控的区域坐标 (left, top, right, bottom)
# left:左上角距离屏幕左侧的距离(像素)
# top:左上角距离屏幕上方的距离(像素)
# right:右下角距离屏幕左侧的距离(像素)
# bottom:右下角距离屏幕上方的距离(像素)
region_to_monitor = (0, 0, 100, 100)
monitor_screen_region_changes(region_to_monitor)
如何获得坐标?截图软件一般都有标注像素。
运行
?