首页 > 解决方案 > 有没有办法将图像存储在缓存中,而不是每次都从文件中读取?

问题描述

在 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'

标签: python

解决方案


推荐阅读