'image path is valid but pyautogui.locateOnScreen is unable to read image file?

import cv2
import pyautogui

if __name__ == '__main__':
    template = cv2.imread("Images/新增.png", 0)
    pos = pyautogui.locateOnScreen("Images/新增.png")

cv2 can open the image just fine, I checked the permission and it is same as any images on computer. However pyautogui returns the following error, which says the file is missing, has improper permissions, or is an unsupported or invalid format.

[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('Images/新增.png'): can't open/read file: check file path/integrity
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('Images/新增.png'): can't open/read file: check file path/integrity
Traceback (most recent call last):
  File "E:\Python代码\HealthCheckRunner\HealthCheck.py", line 82, in <module>
    pos = pyautogui.locateOnScreen("Images/新增.png")
  File "F:\HealthCheckRunner\lib\site-packages\pyautogui\__init__.py", line 175, in wrapper
    return wrappedFunction(*args, **kwargs)
  File "F:\HealthCheckRunner\lib\site-packages\pyautogui\__init__.py", line 213, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
  File "F:\HealthCheckRunner\lib\site-packages\pyscreeze\__init__.py", line 373, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
  File "F:\HealthCheckRunner\lib\site-packages\pyscreeze\__init__.py", line 353, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
  File "F:\HealthCheckRunner\lib\site-packages\pyscreeze\__init__.py", line 207, in _locateAll_opencv
    needleImage = _load_cv2(needleImage, grayscale)
  File "F:\HealthCheckRunner\lib\site-packages\pyscreeze\__init__.py", line 170, in _load_cv2
    raise IOError("Failed to read %s because file is missing, "
OSError: Failed to read Images/新增.png because file is missing, has improper permissions, or is an unsupported or invalid format

Process finished with exit code 1


absolute path returns the same error too

pos = pyautogui.locateOnScreen("E:/Python代码/HealthCheckRunner/Images/新增.png")
--------------------------
OSError: Failed to read E:/Python代码/HealthCheckRunner/Images/新增.png because file is missing, has improper permissions, or is an unsupported or invalid format



Solution 1:[1]

The path is relative, but not relative to the file where imread() is used, it is relative from the folder where the py command was run

#Directories
.
? test
  ? visual1.py
  ? visual2.py
  ? data
    ? 1.jpg
    ? 2.jpg


#./test/visual1.py
import cv2

img_rgb = cv2.imread('data/2.jpg')
template = cv2.imread('data/1.jpg')

#./test/visual2.py
import cv2

img_rgb = cv2.imread('test/data/2.jpg')
template = cv2.imread('test/data/1.jpg')

if you are in the main project folder and run:

py test/visual1.py

you will get error, but if you run:

py test/visual2.py

everything will be fine.

Solution 2:[2]

I cannot comment yet then please bear it. Try to change the name of file . I think in the converting of file name for library some characters can not be recognized. Do that comment the result and I will edit my post.

Solution 3:[3]

As the error states did you try changing the permissions? Or try putting ./ infront of the folder

eg: pos = pyautogui.locateOnScreen("./Images/??.png")

Solution 4:[4]

Please try to install opencv-python library.

This is the line to install pip install opencv-python. I tried to change the name off arquive too, sometimes is confuse for the system. I was trying it and now is working for me.

Solution 5:[5]

You use Chinese in the code! Even if you change file encodings to UTF-8, it will report error still. You'd better change it to an English name instead of using Chinese.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 MrHola21
Solution 4
Solution 5 Kartone