pyautogui脱离屏幕基于图片的图像定位
用过pyautogui的同学应该都知道,locateOnScreen可以传入一张图片与当前屏幕(截屏)进行匹配,但是我的需求是能基于已经截屏的图片来进行图像定位,看了pyautogui的源码没有相关的接口,突发奇想自己定义一个。
首先,参考locateOnScreen的源码
/usr/local/lib/python3.9/site-packages/pyautogui/__init__.py
@raisePyAutoGUIImageNotFoundExceptiondef locateOnScreen(*args, **kwargs):return pyscreeze.locateOnScreen(*args, **kwargs)
/usr/local/lib/python3.9/site-packages/pyscreeze/__init__.py
def locateOnScreen(image, minSearchTime=0, **kwargs):"""TODO - rewrite thisminSearchTime - amount of time in seconds to repeat takingscreenshots and trying to locate a match. The default of 0 performsa single search."""start = time.time()while True:try:screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.retVal = locate(image, screenshotIm, **kwargs)try:screenshotIm.fp.close()except AttributeError:# Screenshots on Windows won't have an fp since they came from# ImageGrab, not a file. Screenshots on Linux will have fp set# to None since the file has been unlinkedpassif retVal or time.time() - start > minSearchTime:return retValexcept ImageNotFoundException:if time.time() - start > minSearchTime:if USE_IMAGE_NOT_FOUND_EXCEPTION:raiseelse:return None
从上面代码可以看出,截屏的图像对象是通过screenshot得到的
screenshotIm = screenshot(region=None)
# set the screenshot() function based on the platform running this module
if sys.platform.startswith('java'):raise NotImplementedError('Jython is not yet supported by PyScreeze.')
elif sys.platform == 'darwin':screenshot = _screenshot_osx
elif sys.platform == 'win32':screenshot = _screenshot_win32
else: # TODO - Make this more specific. "Anything else" does not necessarily mean "Linux".screenshot = _screenshot_linux
从上面代码可以看出screenshot是平台相关的接口,因为我是mac平台,所以参考了_screenshot_osx的实现
def _screenshot_osx(imageFilename=None, region=None):"""TODO"""# TODO - use tmp name for this file.if imageFilename is None:tmpFilename = 'screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))else:tmpFilename = imageFilenamesubprocess.call(['screencapture', '-x', tmpFilename])im = Image.open(tmpFilename)if region is not None:assert len(region) == 4, 'region argument must be a tuple of four ints'region = [int(x) for x in region]im = im.crop((region[0], region[1], region[2] + region[0], region[3] + region[1]))os.unlink(tmpFilename) # delete image of entire screen to save cropped versionim.save(tmpFilename)else:# force loading before unlinking, Image.open() is lazyim.load()if imageFilename is None:os.unlink(tmpFilename)return im
从上面代码可以看出,实际上截屏的接口的实现也很简单,先使用命令截屏保存图片,再用Image.open()打开图片创建一个对象im,然后在im.load()即可,接口返回的是对象im。
由此,可以看出,只要外部传入一个图片文件路径,然后使用Image.open()得到im就可以进行图像定位了。
新增代码如下:
/usr/local/lib/python3.9/site-packages/pyautogui/__init__.py
@raisePyAutoGUIImageNotFoundExceptiondef locateOnImage(*args, **kwargs):return pyscreeze.locateOnImage(*args, **kwargs)
/usr/local/lib/python3.9/site-packages/pyscreeze/__init__.py
def locateOnImage(scr_image, target_image, minSearchTime=0, **kwargs):"""TODO - rewrite thisminSearchTime - amount of time in seconds to repeat takingscreenshots and trying to locate a match. The default of 0 performsa single search."""start = time.time()while True:try:im = Image.open(target_image)im.load()#screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.retVal = locate(scr_image, im, **kwargs)try:im.fp.close()except AttributeError:# Screenshots on Windows won't have an fp since they came from# ImageGrab, not a file. Screenshots on Linux will have fp set# to None since the file has been unlinkedpassif retVal or time.time() - start > minSearchTime:return retValexcept ImageNotFoundException:if time.time() - start > minSearchTime:if USE_IMAGE_NOT_FOUND_EXCEPTION:raiseelse:return None
src_image就相当于原本locateOnScreen的image,target_image就是要基于此定位的图片,然后把原本的
screenshotIm = screenshot(region=None)
换成
im = Image.open(target_image)
im.load()
现在就可以使用自定义接口来进行图片和图片的匹配定位了
x, y = locate_on_image('chrome.png','screen.png', confidence=0.9)
print(x, y)
输出结果:
679 1619
chrome.png
screen.png
pyautogui脱离屏幕基于图片的图像定位相关推荐
- 【PyAutoGUI操作指南】05 屏幕截图与图像定位:截图+定位单个目标+定位全部目标+灰度匹配+像素匹配+获取屏幕截图中像素的RGB颜色
6 屏幕截图与图像定位 PyAutoGUI可以拍摄屏幕截图,将其保存到文件中,并在屏幕中定位图像.OSX使用操作系统附带的screencapture命令.Linux使用scrot命令,可以通过运行su ...
- CVPR 2021 | 澳洲国立大学提出基于模型的图像风格迁移
©作者|侯云钟 学校|澳洲国立大学博士生 研究方向|计算机视觉 本文从另外一个角度解读,澳洲国立大学郑良老师实验室 CVPR 2021 新工作.一般而言,我们需要同时利用两张图片完成图像的风格迁移(s ...
- ECCV 2020 Spotlight | 图像定位上的细粒化区域相似性自监督
©PaperWeekly · 作者|葛艺潇 学校|香港中文大学博士生 研究方向|图像检索.图像生成等 本文介绍一篇我们发表于 ECCV 2020 的论文,很荣幸该论文被收录为 spotlight pr ...
- ECCV2020 Spotlight | 图像定位上的细粒化区域相似性自监督
本文转载自知乎,作者为香港中文大学MMLab博士生葛艺潇,已获作者授权转载. https://zhuanlan.zhihu.com/p/169596514 本文介绍一篇我们发表于ECCV 2020的论 ...
- 深度学习(二十)基于Overfeat的图片分类、定位、检测
基于Overfeat的图片分类.定位.检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187881 作者:hjimce 一.相关理论 本篇博 ...
- c语言 投影法图像定位,基于抛物线拟合和边缘点搜索的虹膜精确定位
摘 要: 虹膜的定位包括瞳孔(内圆)和虹膜外圆的定位.该方法首先预判断瞳孔中心和半径截取目标区域小图,然后对目标区域提取边缘采用最小二乘抛物线拟合,算出左右固定区域内边缘点极值点坐标,得到瞳孔初始的 ...
- PyAutoGui图像操作(二):图像定位不稳定解决方案
一,PyAutoGui介绍 PyAutoGUI是Python功能强大的UI自动化库,其目的是可以用程序自动控制鼠标和键盘操作,主要用来实现PC端的UI自动化. ①有鼠标控制.键盘操作.屏幕截图.图片定 ...
- java图片降噪_Java基于opencv实现图像数字识别(四)—图像降噪
Java基于opencv实现图像数字识别(四)-图像降噪 我们每一步的工作都是基于前一步的,我们先把我们前面的几个函数封装成一个工具类,以后我们所有的函数都基于这个工具类 这个工具类呢,就一个成员变量 ...
- CV之FR:基于DIY人脸图像数据集(每人仅需几张人脸图片训练)利用Hog方法提取特征和改进的kNN算法实现人脸识别并标注姓名(标注文本标签)—(准确度高达100%)
CV之FR:基于DIY人脸图像数据集(每人仅需几张人脸图片训练)利用Hog方法提取特征和改进的kNN算法实现人脸识别并标注姓名(标注文本标签)-(准确度高达100%) 目录 基于DIY人脸图像数据集( ...
最新文章
- 微信小程序实现滑动tab切换和点击tab切换并显示相应的数据(附源代码)
- 你和人工智能的对话,正在被人工收听
- 【从零学习OpenCV】4Ubuntu系统中安装OpenCV 4
- 小猿圈解析vue数据双向绑定的缺点
- 在WinDBG中查看调用栈的命令
- 零食嘴----美食领域的美丽说
- MyBatis学习总结(8)——Mybatis3.x与Spring4.x整合
- asp.net中的validaterequest属性与安全性
- Celery-一个会做异步任务,定时任务的芹菜
- mysql数据库面试题大全(持续更新)
- wireshark保存文件集-解决pcap文件过大,打开极慢的问题
- Xmind思维导图 常用快捷键使用
- 很短,很文艺,很唯美的英语美句
- 华为内部访谈:凡是溜须拍马的员工,一律拿掉
- 数据挖掘笔试面试(5)
- win10 uwp 打开文件管理器选择文件
- react路由鉴权 / 路由守卫
- 关于RetroPie游戏模拟器添加游戏的过程分享。
- FLUKE网线测试仪在Wi-Fi 6时代的应用
- Java 输入月份判断该月份有多少天
热门文章
- if语句判断真假的几种情况
- 第七章 MyBatis 缓存配置
- docker配置Java环境
- viper4android md,【超级街霸4安卓版】超级街霸4安卓完整移植版游戏下载-街机中国...
- oracle until freed,Oracle中报错Connect internal only, until freed
- chinapay java_ECSHOP 银联电子支付(ChinaPay)插件 掉用JAVA签名
- 这两天学会了怎么给线上锡,怎么剥线,怎么焊接两根线。
- python中箭头是什么意思_python流程框里面的箭头是什么意思
- 市场调研-全球与中国在线软件文档工具市场现状及未来发展趋势
- 美丽即可用抑或可用即美丽?