asweigart / pyscreeze

PyScreeze is a simple, cross-platform screenshot module for Python 2 and 3.
BSD 3-Clause "New" or "Revised" License
194 stars 96 forks source link

Loop locateCenterOnScreen() Will lead to OutOfMemoryError #77

Open hoixding opened 3 years ago

hoixding commented 3 years ago

This is error imformation:

Traceback (most recent call last): File "C:\Users\A\Desktop\waterRPA\waterRPA.py", line 158, in mainWork(sheet1) File "C:\Users\A\Desktop\waterRPA\waterRPA.py", line 99, in mainWork mouseClick(1,"left",img,reTry) File "C:\Users\A\Desktop\waterRPA\waterRPA.py", line 13, in mouseClick location=pyautogui.locateCenterOnScreen(img) File "C:\Users\A\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyautogui__init.py", line 175, in wrapper return wrappedFunction(*args, **kwargs) File "C:\Users\A\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyautogui\init.py", line 207, in locateCenterOnScreen return pyscreeze.locateCenterOnScreen(*args, **kwargs) File "C:\Users\A\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyscreeze\init.py", line 413, in locateCenterOnScreen coords = locateOnScreen(image, **kwargs) File "C:\Users\A\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyscreeze\init.py", line 373, in locateOnScreen retVal = locate(image, screenshotIm, **kwargs) File "C:\Users\A\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyscreeze\init.py", line 353, in locate points = tuple(locateAll(needleImage, haystackImage, **kwargs)) File "C:\Users\A\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyscreeze\init__.py", line 229, in _locateAll_opencv result = cv2.matchTemplate(haystackImage, needleImage, cv2.TM_CCOEFF_NORMED) cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\core\src\alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 199209624 bytes in function 'cv::OutOfMemoryError'

And the picture use to locateCenterOnScreen() just 16.5KB.But it throw OutOfMemoryError.

while Ture:
  location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
  if location is not None:
    pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
    break
  print("Not found,retry after 0.1s")
  time.sleep(0.1)

This error will happend after 10 times or more.

python 3.8 MouseInfo 0.1.3 numpy 1.21.4 opencv-python 4.5.4.60 Pillow 8.4.0 pip 21.3.1 PyAutoGUI 0.9.53 PyGetWindow 0.0.9 PyMsgBox 1.0.9 pyperclip 1.8.2 PyRect 0.1.4 PyScreeze 0.1.28 pytweening 1.0.4 setuptools 41.2.0 xlrd 2.0.1

CarmaisTech commented 2 years ago

I have the same problem. It happens when i use pyautogui.locateCenterOnScreen(path_img) on loop. My image has only 24.4Kb. In my case happened in iteration 1784. I would do a total of 3000 iterations.