파이썬
[CV] OpenCV cv2 imread() 할 때 can't open/read file: check file path/integrity 오류 해결
Tiabet
2024. 12. 4. 21:37
요즘 이미지 처리 작업을 하고 있어서 cv2 라이브러리를 많이 사용하는데, 하루는 이런 오류가 발생했다.
integrity가 맞지 않아서 오류가 발생하는 것.
구글링을 해본 결과 해답을 얻을 수 있었다.
https://stackoverflow.com/questions/72022176/warning-cant-open-read-file-check-file-path-integrity
OpenCV 라이브러리로 파일을 읽을 때는 파일 이름에 특수문자가 있어서는 안 된다는 것. ( _ 언더바는 된다.)
특수문자 뿐만이 아니라 파일 이름에 한글도 있어서는 안 되었다. (디렉토리도 마찬가지)
즉 다수의 한국어로 된 파일을 갖고 있는 나는 이걸 해결해주기 위해 또 다른 작업을 해야만 했다...
import os
# Define the directory containing PNG files
directory = "path/to/your/directory" # Replace with your directory path
# Define a renaming pattern
new_name_prefix = "image" # Replace with your desired prefix
# Iterate over all files in the directory
for index, filename in enumerate(os.listdir(directory)):
if filename.endswith(".png"): # Only process .png files
# Generate new file name
new_name = f"{new_name_prefix}_{index+1}.png"
# Get the full path for the old and new names
old_file_path = os.path.join(directory, filename)
new_file_path = os.path.join(directory, new_name)
# Rename the file
os.rename(old_file_path, new_file_path)
print("File renaming complete!")
GPT4o 로 빠르게 생성한 파일 이름 일괄 변경 방법이다. 구글이나 네이버를 치면 뭐 쭉 드래그해서 한 번에 바꾸기, 네이버 프로그램 사용하기 등이 나오는데 드래그해서 일괄 변경하는 방법은 윈도우 시스템상 같은 파일 이름으로 변경하면 소괄호 ()로 구분하는 시스템 때문에 적용이 불가능하다. OpenCV는 소괄호도 특수문자로 취급하여 파일을 읽지 못한다. 프로그램을 깔 바에는 그냥 이렇게 파이썬으로 한 번에 바꾸는 게 훨씬 편한 것 같다.