是否可以 pyscreeze.locate(needleImage, haystackImage): 而不是每次都从文件中读取 haystackImage?
Is it possible to pyscreeze.locate(needleImage, haystackImage): without reading haystackImage from a file each time?
我目前正在将 PyAutoGUI 用于在 haystackImage
上搜索 needleImage
的定位功能。文档提供的示例采用图像路径。但是,我有一个函数可以将一系列 needleImage
与单个 haystackImages
进行比较,并且读取同一个图像文件超过需要检查的次数是非常低效的。
有没有办法避免每次都读取heystackImage
?如果没有,是否有任何替代 pyautogui/pyscreeze 的使用 bufferedImage 的定位功能?
...
checks = {
"recieve.png": 'recieve',
"next.png": 'next',
"start.png": 'start',
"black.png": 'loading',
"loading.png": 'loading',
"gear.png": 'home',
"factory.png": 'factory',
"bathtub.png": 'bathtub',
"refit.png": 'refit',
"supply.png": 'supply',
"dock.png": 'dock',
# SPE
"spepage.png": 'spe',
"expeditionpage.png": 'expedition',
"sortiepage.png": 'sortie',
"practice.png": 'practice',
"practiceinfo.png": 'practice',
"oquest.png": 'quest',
"quest.png": 'quest'
}
for key in checks:
if (detect(key, cache=True)):
return checks[key]
def detect(imgDir, confidence=0.85, cache=False):
if (pyautogui.locate(os.path.join('images', imgDir), 'images\capture.jpeg', confidence=confidence)) is not None:
return True
else:
return False
pyautogui.locate()
也接受 numpy 数组和 PIL 图像作为输入。您可以将 haystack 图像读入 numpy 数组 (BGR) 或 PIL 图像,然后传递它而不是文件名。
def detect(imgDir, haystackImage, confidence=0.85, cache=False):
if (pyautogui.locate(os.path.join('images', imgDir), haystackImage, confidence=confidence)) is not None:
return True
else:
return False
from matplotlib import image
hsImage = image.imread('images\capture.jpeg')
hsImage = hsImage[:,:,::-1] # convert RGB to BGR
detect('needleImg.png', hsImage, cache=True)
# Alternate method
from PIL import Image
hsImage = Image.open('images\capture.jpeg')
detect('needleImg.png', hsImage, cache=True)
第二种方法可能比第一种方法慢,因为 pyautogui.locate()
最终将 PIL 图像加载为 numpy 数组,这需要额外的处理。
我目前正在将 PyAutoGUI 用于在 haystackImage
上搜索 needleImage
的定位功能。文档提供的示例采用图像路径。但是,我有一个函数可以将一系列 needleImage
与单个 haystackImages
进行比较,并且读取同一个图像文件超过需要检查的次数是非常低效的。
有没有办法避免每次都读取heystackImage
?如果没有,是否有任何替代 pyautogui/pyscreeze 的使用 bufferedImage 的定位功能?
...
checks = {
"recieve.png": 'recieve',
"next.png": 'next',
"start.png": 'start',
"black.png": 'loading',
"loading.png": 'loading',
"gear.png": 'home',
"factory.png": 'factory',
"bathtub.png": 'bathtub',
"refit.png": 'refit',
"supply.png": 'supply',
"dock.png": 'dock',
# SPE
"spepage.png": 'spe',
"expeditionpage.png": 'expedition',
"sortiepage.png": 'sortie',
"practice.png": 'practice',
"practiceinfo.png": 'practice',
"oquest.png": 'quest',
"quest.png": 'quest'
}
for key in checks:
if (detect(key, cache=True)):
return checks[key]
def detect(imgDir, confidence=0.85, cache=False):
if (pyautogui.locate(os.path.join('images', imgDir), 'images\capture.jpeg', confidence=confidence)) is not None:
return True
else:
return False
pyautogui.locate()
也接受 numpy 数组和 PIL 图像作为输入。您可以将 haystack 图像读入 numpy 数组 (BGR) 或 PIL 图像,然后传递它而不是文件名。
def detect(imgDir, haystackImage, confidence=0.85, cache=False):
if (pyautogui.locate(os.path.join('images', imgDir), haystackImage, confidence=confidence)) is not None:
return True
else:
return False
from matplotlib import image
hsImage = image.imread('images\capture.jpeg')
hsImage = hsImage[:,:,::-1] # convert RGB to BGR
detect('needleImg.png', hsImage, cache=True)
# Alternate method
from PIL import Image
hsImage = Image.open('images\capture.jpeg')
detect('needleImg.png', hsImage, cache=True)
第二种方法可能比第一种方法慢,因为 pyautogui.locate()
最终将 PIL 图像加载为 numpy 数组,这需要额外的处理。