PyScreeze 基本使用(3)

本文详细介绍了PyScreeze库中用于屏幕定位和像素匹配的函数,包括locateOnScreen、locateCenterOnScreen、locateAllOnScreen、locate、locateAll等,以及灰度匹配和像素匹配的方法。通过示例展示了如何在屏幕或图像中查找特定图像,并获取其位置信息。同时,讲解了使用grayscale=True参数来提高定位速度以及如何验证单个像素颜色的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PyScreeze 基本使用(3)

定位函数总结

   “定位”函数,都从屏幕的左上角(或图像)开始,然后往右,然后向下寻找。参数可以是:

  • locateOnScreen(image, grayscale=False)
    返回屏幕上第一个找到的图像实例的坐标(left, top, width, height)。如果未在屏幕上找到,则引发ImageNotFoundException

  • locateCenterOnScreen(image, grayscale=False)
    返回屏幕上第一个找到的图像实例中心的(x,y)坐标。如果未在屏幕上找到,则引发ImageNotFoundException

  • locateAllOnScreen(image, grayscale=False)
    返回一个生成器,该生成器包含在屏幕上找到图像的位置生成元组(left, top, width, height)

  • locate(needleImage, haystackImage, grayscale=False)
    返回在haystackImage中找到的needleImage(left, top, width, height)坐标。如果未找到,则引发ImageNotFoundException

  • locateAll(needleImage, haystackImage, grayscale=False)
    返回一个生成器,该生成器包含在haystackImage中找到的needleImage(left, top, width, height)坐标。如果未找到,则引发ImageNotFoundException

   “LocationAll”函数可以用于for循环或传递给list()

>>> import pyscreeze
>>> for pos in pyscreeze.locateAllOnScreen('someButton.png')
...   print(pos)
...
(1101, 252, 50, 50)
(59, 481, 50, 50)
(1395, 640, 50, 50)
(1838, 676, 50, 50)
>>> list(pyscreeze.locateAllOnScreen('someButton.png'))
[(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]

Grayscale Matching

   可以传递一个参数grayscale=True给定位函数,以加快定位速度,但是可能会导致定位错误。

>>> import pyscreeze
>>> button7location = pyscreeze.locateOnScreen('calc7key.png', grayscale=True)
>>> button7location
(1416, 562, 50, 41)

Pixel Matching 像素匹配

   若要获取屏幕截图中像素的RGB颜色,使用Image对象的getpixel()方法:

>>> import pyscreeze
>>> im = pyscreeze.screenshot()
>>> im.getpixel((100, 200))
(130, 135, 144)

   或者直接调用PyScreezepixel()

>>> import pyscreeze
>>> pyscreeze.pixel(100, 200)
(130, 135, 144)

   如果只需要验证单个像素是否与给定像素匹配,则调用像素MatchesColor()函数,将其表示的颜色的X坐标、Y坐标和RGB元组传递给它:

>>> import pyscreeze
>>> pyscreeze.pixelMatchesColor(100, 200, (130, 135, 144))
True
>>> pyscreeze.pixelMatchesColor(100, 200, (0, 0, 0))
False

   可选tolerance关键字参数指定每个红色、绿色和蓝色值在匹配时的变化程度:

>>> import pyscreeze
>>> pyscreeze.pixelMatchesColor(100, 200, (130, 135, 144))
True
>>> pyscreeze.pixelMatchesColor(100, 200, (140, 125, 134))
False
>>> pyscreeze.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10)
True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值