Cant open/read file: check file path/integrity

OpenCV-Python — is a Python bindings library for solving computer vision problems. 
cv2.imread () loads an image from the specified file. If the image cannot be read (due to a missing file, incorrect permissions, unsupported or incorrect format) then this method returns an empty matrix.

Show

Syntax: cv2. imread (path, flag)

Parameters:
path: A string representing the path of the image to be read.
flag: It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLOR

Return Value: This method returns an image that is loaded from the specified file.

Note . The image must be in the working directory or the full path to the image must be specified.

All three types of flags are described below:

cv2.IMREAD_COLOR: It specifies to load a color image. Any transparency of image will be neglected. It is the default flag. Alternatively, we can pass integer value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag.
cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha channel. Alternatively, we can pass integer value -1 for this flag.

Image is used for all examples below:

Cant open/read file: check file path/integrity

Example # 1: Using the default flag

# Python program to explain the cv2.imread () method

 
# cv2 import

import cv2

 
# path

path = r ’ C: UsersRajnishDesktoppythonengineering.png’

 
# Using the cv2.imread () method

img = cv2.imread (path)

 
# Display images

cv2.imshow ( ’image’ , img)

Output:

Cant open/read file: check file path/integrity

Example # 2:
Loading an image in grayscale

# Python program to explain the cv2.imread () method

 
# cv2 import

import cv2

 
# path

path = r ’C: UsersRajnishDesktoppythonengineering.png ’

  
# Using the cv2.imread () method
# Using 0 to read the image in grayscale

img = cv2.imread (path, 0 )

 
# Display image

cv2.imshow ( ’image’ , img)

Output:

오류 원인

Cant open/read file: check file path/integrity

cv2.imread가 파일 경로 안에 있는 '문서'라는 한글을 읽어오지 못하고 에러를 내보냄

오류

[ WARN:] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('C:/Users/kimsu/OneDrive/臾몄꽌/GitHub/-/ImagesAttendance/Bill_Gates.jpg'):
can't open/read file: check file path/integrity

해결 방법

cv2.imread를 바로 쓰지 않고
경로를 np.fromfile로 np.uint8로 컴퓨터가 읽을수 있게 만들어 준 다음
cv2.imdecode로 이미지 파일을 가져온다


path = 'C:/Users/kimsu/OneDrive/문서/GitHub/-/ImagesAttendance'
images = []

for cl in myList:
    full_path = path+'/'+cl #경로 전체를 가져오고
    img_array = np.fromfile(full_path, np.uint8) # 컴퓨터가 읽을수 있게 넘파이로 변환
    curImg = cv2.imdecode(img_array,  cv2.IMREAD_COLOR) #이미지를 읽어옴
    images.append(curImg)

Cant open/read file: check file path/integrity

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

运行出错代码:

import cv2
import numpy as np

image = cv2.imread('C:/Pictures/桌面背景图片切换/wallhaven-6oq1k7.jpg', cv2.IMREAD_COLOR)
cv2.imshow("test", image)
cv2.waitKey(0)

报错内容:

[ WARN:] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('C:/Pictures/桌面背景图片切换/wallhaven-6oq1k7.jpg'): **can't open/read file: check file path/integrity**
Traceback (most recent call last):
  File "D:/Code/DeepLearning/test/main.py", line 13, in <module>
    cv2.imshow("test", image)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'


Process finished with exit code 1

报错原因:路径中出现中文字符
解决办法:
1.修改路径
2.修改代码,修改后代码如下:

import cv2
import numpy as np
# 读取图像,解决imread不能读取中文路径的问题
def cv_imread(file_path):
    cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
    # im decode读取的是rgb,如果后续需要opencv处理的话,需要转换成bgr,转换后图片颜色会变化
    cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
    return cv_img


if __name__ == '__main__':
    path = 'C:/Users/18724/Pictures/桌面背景图片切换/wallhaven-6oq1k7.jpg'
    img = cv_imread(path)
    cv2.namedWindow("test", cv2.WINDOW_AUTOSIZE)
    cv2.imshow("test", img)
    cv2.waitKey(0)
    # 保存到当前运行目录下
    cv2.imencode('.jpg', img)[1].tofile('凝光.jpg')

参考链接:
1.https://blog.csdn.net/liuqinshouss/article/details/78696032
2.https://www.zhihu.com/question/47184512
3.https://www.zhihu.com/question/67157462/answer/251754530