How to use region_screenshot method in ATX

Best Python code snippet using ATX

pyautogui_demo.py

Source:pyautogui_demo.py Github

copy

Full Screen

1import pyautogui2import time3pyautogui.PAUSE = 1 # 调用在执行动作后暂停的秒数,只能在执行一些pyautogui动作后才能使用,建议用time.sleep4pyautogui.FAILSAFE = True # 启用自动防故障功能,左上角的坐标为(0,0),将鼠标移到屏幕的左上角,来抛出failSafeException异常5def test():6 # 判断(x,y)是否在屏幕上7 x, y = 122, 2448 pyautogui.onScreen(x, y) # 结果为true9 width, height = pyautogui.size() # 屏幕的宽度和高度10 print(width, height)11# 鼠标控制12def mouse_control():13 currentMouseX, currentMouseY = pyautogui.position() # 鼠标当前位置14 print(currentMouseX, currentMouseY)15 # 控制鼠标移动,duration为持续时间16 for i in range(2):17 pyautogui.moveTo(100, 100, duration=0.25) # 移动到 (100,100)18 pyautogui.moveTo(200, 100, duration=0.25)19 pyautogui.moveTo(200, 200, duration=0.25)20 pyautogui.moveTo(100, 200, duration=0.25)21 for i in range(2):22 pyautogui.moveRel(50, 0, duration=0.25) # 从当前位置右移100像素23 pyautogui.moveRel(0, 50, duration=0.25) # 向下24 pyautogui.moveRel(-50, 0, duration=0.25) # 向左25 pyautogui.moveRel(0, -50, duration=0.25) # 向上26 # 按住鼠标左键,把鼠标拖拽到(100, 200)位置27 pyautogui.dragTo(100, 200, button='left')28 # 按住鼠标左键,用2秒钟把鼠标拖拽到(300, 400)位置29 pyautogui.dragTo(300, 400, 2, button='left')30 # 按住鼠标左键,用0.2秒钟把鼠标向上拖拽31 pyautogui.dragRel(0, -60, duration=0.2)32 # pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')33 # 其中,button属性可以设置成left,middle和right。34 pyautogui.click(10, 20, 2, 0.25, button='left')35 pyautogui.click(x=100, y=200, duration=2) # 先移动到(100, 200)再单击36 pyautogui.click() # 鼠标当前位置点击一下37 pyautogui.doubleClick() # 鼠标当前位置左击两下38 pyautogui.doubleClick(x=100, y=150, button="left") # 鼠标在(100,150)位置左击两下39 pyautogui.tripleClick() # 鼠标当前位置左击三下40 pyautogui.mouseDown() # 鼠标左键按下再松开41 pyautogui.mouseUp()42 pyautogui.mouseDown(button='right') # 按下鼠标右键43 pyautogui.mouseUp(button='right', x=100, y=200) # 移动到(100, 200)位置,然后松开鼠标右键44 # scroll函数控制鼠标滚轮的滚动,amount_to_scroll参数表示滚动的格数。正数则页面向上滚动,负数则向下滚动45 # pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY)46 pyautogui.scroll(5, 20, 2)47 pyautogui.scroll(10) # 向上滚动10格48 pyautogui.scroll(-10) # 向下滚动10格49 pyautogui.scroll(10, x=100, y=100) # 移动到(100, 100)位置再向上滚动10格50 # 缓动/渐变函数可以改变光标移动过程的速度和方向。通常鼠标是匀速直线运动,这就是线性缓动/渐变函数。51 # PyAutoGUI有30种缓动/渐变函数,可以通过pyautogui.ease*?查看。52 # 开始很慢,不断加速53 pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)54 # 开始很快,不断减速55 pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)56 # 开始和结束都快,中间比较慢57 pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)58 # 一步一徘徊前进59 pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)60 # 徘徊幅度更大,甚至超过起点和终点61 pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)62# 获取鼠标位置63def get_mouse_positon():64 time.sleep(5) # 准备时间65 print('开始获取鼠标位置')66 try:67 for i in range(10):68 # Get and print the mouse coordinates.69 x, y = pyautogui.position()70 positionStr = '鼠标坐标点(X,Y)为:{},{}'.format(str(x).rjust(4), str(y).rjust(4))71 pix = pyautogui.screenshot().getpixel((x, y)) # 获取鼠标所在屏幕点的RGB颜色72 positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(73 3) + ')'74 print(positionStr)75 time.sleep(0.5) # 停顿时间76 except:77 print('获取鼠标位置失败')78# 键盘控制79def key_board_control():80 pyautogui.typewrite('Hello world!') # 输入Hello world!字符串81 pyautogui.typewrite('Hello world!', interval=0.25) # 每次输入间隔0.25秒,输入Hello world!82 pyautogui.press('enter') # 按下并松开(轻敲)回车键83 pyautogui.press(['left', 'left', 'left', 'left']) # 按下并松开(轻敲)四下左方向键84 pyautogui.keyDown('shift') # 按下`shift`键85 pyautogui.keyUp('shift') # 松开`shift`键86 pyautogui.keyDown('shift')87 pyautogui.press('4')88 pyautogui.keyUp('shift') # 输出 $ 符号的按键89 pyautogui.hotkey('ctrl', 'v') # 组合按键(Ctrl+V),粘贴功能,按下并松开'ctrl'和'v'按键90 # pyautogui.KEYBOARD_KEYS数组中就是press(),keyDown(),keyUp()和hotkey()函数可以输入的按键名称91 pyautogui.KEYBOARD_KEYS = ['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-',92 '.',93 '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',94 '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',95 'l',96 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',97 'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace', 'browserback',98 'browserfavorites', 'browserforward', 'browserhome', 'browserrefresh', 'browsersearch',99 'browserstop', 'capslock', 'clear', 'convert', 'ctrl', 'ctrlleft', 'ctrlright',100 'decimal',101 'del', 'delete', 'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1',102 'f10',103 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 'f21', 'f22',104 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'final', 'fn', 'hanguel',105 'hangul',106 'hanja', 'help', 'home', 'insert', 'junja', 'kana', 'kanji', 'launchapp1', 'launchapp2',107 'launchmail', 'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',108 'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8',109 'num9',110 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn', 'pgup', 'playpause', 'prevtrack',111 'print',112 'printscreen', 'prntscrn', 'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select',113 'separator', 'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract',114 'tab',115 'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',116 'command',117 'option', 'optionleft', 'optionright']118def popups_control():119 # 显示一个简单的带文字和OK按钮的消息弹窗。用户点击后返回button的文字。120 pyautogui.alert(text='', title='', button='OK')121 b = pyautogui.alert(text='要开始程序么?', title='请求框', button='OK')122 print(b) # 输出结果为OK123 # 显示一个简单的带文字、OK和Cancel按钮的消息弹窗,用户点击后返回被点击button的文字,支持自定义数字、文字的列表。124 pyautogui.confirm(text='', title='', buttons=['OK', 'Cancel']) # OK和Cancel按钮的消息弹窗125 pyautogui.confirm(text='', title='', buttons=range(10)) # 10个按键0-9的消息弹窗126 a = pyautogui.confirm(text='', title='', buttons=range(10))127 print(a) # 输出结果为你选的数字128 # 可以输入的消息弹窗,带OK和Cancel按钮。用户点击OK按钮返回输入的文字,点击Cancel按钮返回None。129 pyautogui.prompt(text='', title='', default='')130 # 样式同prompt(),用于输入密码,消息用*表示。带OK和Cancel按钮。用户点击OK按钮返回输入的文字,点击Cancel按钮返回None。131 pyautogui.password(text='', title='', default='', mask='*')132def screen_control():133 pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\my_screenshot.png') # 截全屏并设置保存图片的位置和名称134 im = pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\my_screenshot.png') # 截全屏并设置保存图片的位置和名称135 print(im) # 打印图片的属性136 # 不截全屏,截取区域图片。截取区域region参数为:左上角XY坐标值、宽度和高度137 pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png', region=(0, 0, 300, 400))138 pix = pyautogui.screenshot().getpixel((220, 200)) # 获取坐标(220,200)所在屏幕点的RGB颜色139 positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'140 print(positionStr) # 打印结果为RGB:( 60, 63, 65)141 pix = pyautogui.pixel(220, 200) # 获取坐标(220,200)所在屏幕点的RGB颜色与上面三行代码作用一样142 positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'143 print(positionStr) # 打印结果为RGB:( 60, 63, 65)144 # 如果你只是要检验一下指定位置的像素值,可以用pixelMatchesColor(x,y,RGB)函数,把X、Y和RGB元组值穿入即可145 # 如果所在屏幕中(x,y)点的实际RGB三色与函数中的RGB一样就会返回True,否则返回False146 # tolerance参数可以指定红、绿、蓝3种颜色误差范围147 pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))148 pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10)149 # 获得文件图片在现在的屏幕上面的坐标,返回的是一个元组(top, left, width, height)150 # 如果截图没找到,pyautogui.locateOnScreen()函数返回None151 a = pyautogui.locateOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png')152 print(a) # 打印结果为Box(left=0, top=0, width=300, height=400)153 x, y = pyautogui.center(a) # 获得文件图片在现在的屏幕上面的中心坐标154 print(x, y) # 打印结果为150 200155 x, y = pyautogui.locateCenterOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png') # 这步与上面的四行代码作用一样156 print(x, y) # 打印结果为150 200157 # 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出158 pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png')159 for pos in pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png'):160 print(pos)161 # 打印结果为Box(left=0, top=0, width=300, height=400)162 a = list(pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png'))163 print(a) # 打印结果为[Box(left=0, top=0, width=300, height=400)]164if __name__ == "__main__":...

Full Screen

Full Screen

fimage.py

Source:fimage.py Github

copy

Full Screen

1# import pyautogui2# import cv23# import numpy4# sys_manger = 'photos/xitongguanli.png'5# print(pyautogui.locateCenterOnScreen(sys_manger))6# Privacy_location = auto.locateCenterOnScreen('D://AppsUITest//Pages//111.jpg',grayscale=False) # 传入按钮的图片7# print(Privacy_location)  # 返回屏幕所在位置8#9# im2 = pyautogui.screenshot('my_screenshot.png')10import pyautogui11import time12time.sleep(10)13pyautogui.screenshot(r'D:\workspace\common\photos\my_screenshot.png') # 截全屏并设置保存图片的位置和名称14im = pyautogui.screenshot(r'D:\workspace\common\photos\my_screenshot.png') # 截全屏并设置保存图片的位置和名称15print(im) # 打印图片的属性16# 不截全屏,截取区域图片。截取区域region参数为:左上角XY坐标值、宽度和高度17pyautogui.screenshot(r'D:\workspace\common\photos\region_screenshot.png', region=(0, 0, 300, 400))18pix = pyautogui.screenshot().getpixel((220, 200)) # 获取坐标(220,200)所在屏幕点的RGB颜色19positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'20print(positionStr) # 打印结果为RGB:( 60, 63, 65)21pix = pyautogui.pixel(220, 200) # 获取坐标(220,200)所在屏幕点的RGB颜色与上面三行代码作用一样22positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'23print(positionStr) # 打印结果为RGB:( 60, 63, 65)24# 如果你只是要检验一下指定位置的像素值,可以用pixelMatchesColor(x,y,RGB)函数,把X、Y和RGB元组值穿入即可25# 如果所在屏幕中(x,y)点的实际RGB三色与函数中的RGB一样就会返回True,否则返回False26# tolerance参数可以指定红、绿、蓝3种颜色误差范围27pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))28pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10)29# 获得文件图片在现在的屏幕上面的坐标,返回的是一个元组(top, left, width, height)30# 如果截图没找到,pyautogui.locateOnScreen()函数返回None31a = pyautogui.locateOnScreen(r'D:\workspace\common\photos\region_screenshot.png')32print(a) # 打印结果为Box(left=0, top=0, width=300, height=400)33x, y = pyautogui.center(a) # 获得文件图片在现在的屏幕上面的中心坐标34print(x, y) # 打印结果为150 20035x, y = pyautogui.locateCenterOnScreen(r'D:\workspace\common\photos\region_screenshot.png') # 这步与上面的四行代码作用一样36print(x, y) # 打印结果为150 20037# 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出38pyautogui.locateAllOnScreen(r'D:\workspace\common\photos\region_screenshot.png')39for pos in pyautogui.locateAllOnScreen(r'D:\workspace\common\photos\region_screenshot.png'):40 print(pos)41# 打印结果为Box(left=0, top=0, width=300, height=400)42a = list(pyautogui.locateAllOnScreen(r'D:\workspace\common\photos\region_screenshot.png'))...

Full Screen

Full Screen

pyauto.py

Source:pyauto.py Github

copy

Full Screen

1import pyautogui2import time,datetime3pyautogui.screenshot(r'C:\my_screenshot.png') # 截全屏并设置保存图片的位置和名称4im = pyautogui.screenshot(r'C:\my_screenshot.png') # 截全屏并设置保存图片的位置和名称5print(im) # 打印图片的属性6# 不截全屏,截取区域图片。截取区域region参数为:左上角XY坐标值、宽度和高度7pyautogui.screenshot(r'C:\region_screenshot.png', region=(0, 0, 300, 300))8pix = pyautogui.screenshot().getpixel((220, 200)) # 获取坐标(220,200)所在屏幕点的RGB颜色9positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'10print(positionStr) # 打印结果为RGB:( 60, 63, 65)11pix = pyautogui.pixel(220, 200) # 获取坐标(220,200)所在屏幕点的RGB颜色与上面三行代码作用一样12positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'13print(positionStr) # 打印结果为RGB:( 60, 63, 65)14# 如果你只是要检验一下指定位置的像素值,可以用pixelMatchesColor(x,y,RGB)函数,把X、Y和RGB元组值穿入即可15# 如果所在屏幕中(x,y)点的实际RGB三色与函数中的RGB一样就会返回True,否则返回False16# tolerance参数可以指定红、绿、蓝3种颜色误差范围17pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))18pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10)19# 获得文件图片在现在的屏幕上面的坐标,返回的是一个元组(top, left, width, height)20# 如果截图没找到,pyautogui.locateOnScreen()函数返回None21startTime = datetime.datetime.now()22pyautogui.screenshot(r'C:\region_screenshot.png', region=(0, 0, 300, 300))23time.sleep(10)24endTime = datetime.datetime.now()25sub = endTime.second -startTime.second26print(str(sub)+'秒')27a = pyautogui.locateOnScreen(r'C:\region_screenshot.png') #定位截图的位置 左顶点,宽高28print(a) # 打印结果为Box(left=0, top=0, width=300, height=400)29x, y = pyautogui.center(a) # 获得文件图片在现在的屏幕上面的中心坐标30print(x, y) # 打印结果为150 20031x, y = pyautogui.locateCenterOnScreen(r'C:\region_screenshot.png') #定位截图的中心坐标32print(x, y) # 打印结果为150 20033# 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出34pyautogui.locateAllOnScreen(r'C:\region_screenshot.png')35for pos in pyautogui.locateAllOnScreen(r'C:\region_screenshot.png'):36 print(pos)37# 打印结果为Box(left=0, top=0, width=300, height=400)38a = list(pyautogui.locateAllOnScreen(r'C:\region_screenshot.png'))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ATX automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful