python - 有没有办法将图像存储在缓存中,而不是每次都从文件中读取?
问题描述
在 Java 中,图像可以保存bufferedimage
在缓存中。Python中是否有与此等价的东西?每个请求读取图像文件似乎效率极低。
该函数使用函数stateRecognizer()
检查当前屏幕上是否存在一系列图像getCoord(imgDir)
,并返回相应的状态。
getCoord(key)
返回 4 个整数的列表。如果找不到图像,则getCoord(key)
返回。None
import pyautogui
from PIL import Image
def get_coord(imgDir, confidence=0.85, screenshot=None, cache=False):
if cache is False:
getSC()
if('.png' not in imgDir):
imgDir = imgDir + '.png'
result = pyautogui.locate(os.path.join('images', imgDir),
'images\\capture.jpeg', confidence=confidence)
if (result is not None):
return result.left, result.top, result.left + result.width, result.top + result.height
else:
return None
def state_recognizer(hint=None, confidence=0.85):
getSC() # Cache Once for get_coord()
if hint != None:
try:
if get_coord(os.path.join(hint + '.png'), cache=True, confidence=confidence) is not None:
return hint
except:
pass
checks = {
"recieve.png": 'recieve',
"next.png": 'next',
"start.png": 'start',
"loadingblack.png": 'loading',
"loading.png": 'loading',
"gear.png": 'home',
"factory.png": 'factory',
"bathtub.png": 'bathtub',
"refit.png": 'refit',
"supply.png": 'supply',
"dock.png": 'dock',
"spepage.png": 'spepage',
"expeditionpage.png": 'expedition',
"sortiepage.png": 'sortie',
"oquest.png": 'quest',
"quest.png": 'quest'
}
for key in checks:
if (get_coord(key, cache=True) is not None):
return checks[key]
return 'unknown'
解决方案
推荐阅读
- c# - ASP.Net Core 3.1 升级
- node.js - 覆盖 express-gateway 配置
- java - Java序列化读取枚举到字符串
- delphi - 如何在同一个 cmd 进程中从 cmd 执行不同的命令?
- sql - SQL 子字符串问题 - 删除某些字符并重新结构
- react-native - Importing installed node modules vs importing css or other files in React
- vba - 将 Access 数据库转换为后期绑定以使用 Access 运行时
- c++ - 为什么必须在 C++ 中显式类型转换 malloc 和 calloc?
- sharepoint - 富文本字段的 SharePoint Online 列表格式
- javascript - Angular 和 Firebase 在值变化中获取可观察的数据