1. 鼠标相关
pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
pyautogui.moveRel(0, 50) # move the mouse down 50 pixels.
pyautogui.click() # click the mouse
pyautogui.click(button='right') # right-click the mouse
pyautogui.click(clicks=2) # double-click the left mouse button
pyautogui.doubleClick() # perform a left-button double click
pyautogui.scroll(10) # scroll up 10 "clicks"
2. 键盘相关
#模拟输入信息
pyautogui.typewrite(message='Hello world!',interval=0.5)
pyautogui.press('enter') # press the Enter key
# the hotkey() can be passed several key strings which will be pressed down in order, and then released in reverse order. This code:
pyautogui.hotkey('ctrl', 'v')
3.截图
im = pyautogui.screenshot(region=(0,0, 300, 400))
4. 跟图片进行像素比较
def check_target_img(self, regin, target_img_name):
'''
如果区域跟目标图片大部分一样,return true, other return false
:param regin:
:param target_img_name:
:return:
'''
img = pyautogui.screenshot(region=regin)
pixdata = img.load()
w, h = img.size
no_code = Image.open(target_img_name)
no_code_pixdata = no_code.load()
diff_pix_count = 0
for y in range(h):
for x in range(w):
if pixdata[x, y] != no_code_pixdata[x, y]:
diff_pix_count += 1
# print(diff_pix_count)
if diff_pix_count < 10:
return True
else:
return False
5. 粘贴板相关
import pyperclip
def get_txt_from_board(self):
'''
从剪贴板得到文字
:return:
'''
return pyperclip.paste()
def set_text_to_board(self, text):
"""设置剪贴板文本"""
pyperclip.copy(text)
6.得到图片在给点图片中的位置
def find_img_pos_in_regin(self, img_name, regin):
'''
:param img_name: 要查找的图片名字
:param regin: 查找区域
:return: 图片在中心位置
'''
import cv2
import numpy as np
im = pyautogui.screenshot(region=regin).convert('RGB')
open_cv_image = np.array(im)
# 加载原始RGB图像
# img_rgb = cv2.imread("brow_locate.png")
img_rgb = open_cv_image[:, :, ::-1].copy()
# 创建一个原始图像的灰度版本,所有操作在灰度版本中处理,然后在RGB图像中使用相同坐标还原
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# 加载将要搜索的图像模板
template = cv2.imread(img_name, 0)
# 记录图像模板的尺寸
w, h = template.shape[::-1]
# 使用matchTemplate对原始灰度图像和图像模板进行匹配
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
# 设定阈值
threshold = 0.7
# res大于70%
loc = np.where(res >= threshold)
# 使用灰度图像中的坐标对原始RGB图像进行标记
for pt in zip(*loc[::-1]):
print(pt)
return (pt[0] + w / 2, pt[1] + h / 2)
http://www.waitingfy.com/archives/4742
Post Views: 5
4742
Python | | | Trackback |