I am trying to use mss and opencv to find images on the screen. However, whenever I try to use regions with mss I get this error:
It seems this issue is related to some ressources not being freed: python mss mss.exception.ScreenShotError:
I found out an alternative solution is to not use regions and look on the whole screen but it becomes harder to find the correct image for small images. Also, when modifying the coordinates of the region, the error may disappear for no reason (not working with 600, 600, 1200, 800 but working with 200, 200, 1200, 800 for example).
test.py
from python_imagesearch.imagesearch import *
imagesearcharea("images/fermer.png", 600, 600, 1200, 800)
imagesearch.py
def region_grabber(region):
if is_retina: region = [n * 2 for n in region]
x1 = region[0]
y1 = region[1]
width = region[2]
height = region[3]
region = x1, y1, width, height
with mss.mss() as sct:
return sct.grab(region)
def imagesearcharea(image, x1, y1, x2, y2, precision=0.8, im=None):
if im is None:
im = region_grabber(region=(x1, y1, x2, y2))
if is_retina:
im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
img_rgb = np.array(im)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(image, 0)
if template is None:
raise FileNotFoundError('Image file not found: {}'.format(image))
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val < precision:
return [-1, -1]
return max_loc
My mss version is 6.1, python is 3.7, using windows 10 Do you have any idea of a solution, or do you know an alternative library to replace mss?