pythonマウスシミュレーション
pythonマウスシミュレーション
マウスの座標を簡単に記録し、マウスの下の画像のオフセットを計算することで、マイクロオフセットのインタフェースでマウスの操作ソースリンクをシミュレートするための簡単な位置較正を完了します.
レコードセクション
アクションセクション
マウスの座標を簡単に記録し、マウスの下の画像のオフセットを計算することで、マイクロオフセットのインタフェースでマウスの操作ソースリンクをシミュレートするための簡単な位置較正を完了します.
レコードセクション
# -*- coding:utf-8 -*-
import datetime
import os
from threading import Thread
import pynput
from PIL import ImageGrab
from pynput.keyboard import Key
#
last_time = datetime.datetime.now()
operate_record = []
id = 0
status_flag = True
#
def make_screenshot(x1, y1, x2, y2):
"""
:param x1: x1
:param y1: x1
:param x2: x2
:param y2: y2
:return: None
"""
# id: ID
global id
bbox = (x1, y1, x2, y2)
im = ImageGrab.grab(bbox)
im.save('./pic/' + str(id) + '.png') #
# ,
def on_move(x, y):
global status_flag
if not status_flag:
return status_flag
global last_time
global id
global operate_record
#
if y > 1080:
y = 1080
if x > 1920:
x = 1920
if x < 0:
x = 0
if y < 0:
y = 0
print('Pointer moved to {0}'.format((x, y)))
id += 1
#
if id % 100 == 0 and (x >= 150 or x <= 1920 - 150) and (y > 100 or y < 1080 - 100):
make_screenshot(x - 75, y - 50, x + 75, y + 50)
single = {
'id': id, 'x': x, 'y': y, "event": "move", "button": "", 'action': '',
'time': (datetime.datetime.now() - last_time).total_seconds()}
last_time = datetime.datetime.now()
operate_record.append(single)
#
def on_click(x, y, button, pressed):
global status_flag
if not status_flag:
return status_flag
global last_time
global id
global operate_record
#
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
id += 1
single = {
'id': id, 'x': x, 'y': y, "event": "click", "button": str(button),
'action': 'pressed' if pressed else 'released',
'time': (datetime.datetime.now() - last_time).total_seconds()}
last_time = datetime.datetime.now()
operate_record.append(single)
make_screenshot(x - 75, y - 50, x + 75, y + 50)
#
def on_press(key):
global status_flag
if not status_flag:
return status_flag
#
print('{0} pressed'.format(key))
#
def on_release(key):
global status_flag
global operate_record
#
print('{0} release'.format(key))
# ESC ,
if key == Key.esc:
with open('./record.txt', 'w') as fp:
for i in operate_record:
fp.write(str(i) + '
')
print("end")
status_flag = False
return status_flag
def listener_mouse():
with pynput.mouse.Listener(on_move=on_move, on_click=on_click) as listener:
listener.join()
def listener_key():
with pynput.keyboard.Listener(on_press=on_press, on_release=on_release)as listener:
listener.join()
# 。 , 。 pynput.mouse.Listener.stop, pynput.mouse.Listener.StopException False 。
if __name__ == '__main__':
if not os.path.exists('./pic'):
os.mkdir('./pic')
t1 = Thread(target=listener_mouse)
t2 = Thread(target=listener_key)
t1.start()
t2.start()
アクションセクション
# -*- coding:utf-8 -*-
#
#
import json
import os
import time
from threading import Thread
import cv2
from PIL import ImageGrab
from pynput.keyboard import Key
import pynput
status_flag = True
mouse = pynput.mouse.Controller()
calibration = [0, 0]
def make_screenshot(x1, y1, x2, y2):
"""
:param x1: x1
:param y1: x1
:param x2: x2
:param y2: y2
:return: None
"""
# id: ID
global id
bbox = (x1, y1, x2, y2)
im = ImageGrab.grab(bbox)
im.save('./temp/present.png') #
def identify_pictures(img1Path, img2Path):
img = cv2.imread(img1Path, 0)
img2 = img.copy()
template = cv2.imread(img2Path, 0)
w, h = template.shape[::-1]
meth = 'cv2.TM_CCOEFF'
img = img2.copy()
method = eval(meth)
res = cv2.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
return max_loc
def setPosition(x, y):
global mouse
# , 1.25
mouse.position = (x / 1.25, y / 1.25)
def setLeftPress():
global mouse
mouse.press(pynput.mouse.Button.left)
def setLeftRelease():
global mouse
mouse.release(pynput.mouse.Button.left)
def setRightPress():
global mouse
mouse.press(pynput.mouse.Button.right)
def setRightRelease():
global mouse
mouse.release(pynput.mouse.Button.right)
def on_press(key):
#
print('{0} pressed'.format(key))
def on_release(key):
global status_flag
#
print('{0} release'.format(key))
if key == Key.esc:
status_flag = False
return status_flag
# ,
def calibration_offset(i):
make_screenshot(i['x'] - 150 + calibration[0], i['y'] - 100 + calibration[1], i['x'] + 150 + calibration[0],
i['y'] + 100 + calibration[1])
inner_top_left = identify_pictures('./temp/present.png', './pic/' + str(i['id']) + '.png')
outside = (
i['x'] - 150 + inner_top_left[0] + calibration[0], i['y'] - 100 + inner_top_left[1] + calibration[1])
calibration[0] = outside[0] - (i['x'] - 75)
calibration[1] = outside[1] - (i['y'] - 50)
def operate_mouse():
global status_flag
record = []
with open("./record.txt", 'r') as fp:
for line in fp:
record.append(json.loads(line.replace("\'", "\"")))
for i in record:
# print(i)
if not status_flag:
break
if i['event'] == 'move':
if i['id'] % 100 == 0 and (i['x'] >= 200 or i['x'] <= 1920 - 200) and (i['y'] > 100 or i['y'] < 1080 - 100):
calibration_offset(i)
i['x'] = i['x'] + calibration[0]
i['y'] = i['y'] + calibration[1]
setPosition(i['x'], i['y'])
elif i['event'] == 'click':
calibration_offset(i)
setPosition(i['x'] + calibration[0], i['y'] + calibration[1])
if i['action'] == 'pressed':
if i['button'] == 'Button.left':
setLeftPress()
elif i['button'] == 'Button.right':
setRightPress()
elif i['action'] == 'released':
if i['button'] == 'Button.left':
setLeftRelease()
elif i['button'] == 'Button.right':
setRightRelease()
time.sleep(i['time'])
status_flag = False
print("end")
def listener_key():
with pynput.keyboard.Listener(on_press=on_press, on_release=on_release)as listener:
listener.join()
if __name__ == '__main__':
if not os.path.exists('./temp'):
os.mkdir('./temp')
t1 = Thread(target=operate_mouse)
t2 = Thread(target=listener_key)
t1.start()
t2.start()