为什么 "cv2.imread" 对具有不同名称的图像文件表现不同?
Why "cv2.imread" behaves differently for image files having different names?
我正在尝试编写一个 python 脚本,该脚本应将图像作为输入并裁剪它们,并将裁剪后的图像保存在具有相同(原始)文件名的不同文件夹中。所以我写了下面的代码..
import cv2
#import numpy
import os
import glob
#import sys
for imgfile in glob.glob(r"C:\Users\mainu\Documents\ACADEMIC YEAR 20_21\Master thesis\Implementation\Applications\Apps_with_opencv\Test\*.jpg"):
img= cv2.imread(imgfile)
name = os.path.basename(imgfile)
print('old name :',name)
rows,colmn,channel = img.shape # to know the shape of the image
#cv2.imshow('This image has :' + str(rows) +' number of rows,' + str(colmn) +' number of columns',img) #shows the image
#cv2.waitKey(0) #waits untill any key is pressed
cropped_img = img[400:949,408:1588] # here we are cutting the image
#rows,colmn,channel = cropped_img.shape # to know the shape of the new image
#cv2.imshow('This image has :' + str(rows) +' number of rows,' + str(colmn) +' number of columns',cropped_img) #shows the new image
#cv2.waitKey(0) #waits untill any key is pressed
filename = r"C:\Users\mainu\Documents\ACADEMIC YEAR 20_21\Master thesis\Implementation\Applications\Apps_with_opencv\Test_2/" + name
name_2 = os.path.basename(filename)
print('new name :',name_2)
cv2.imwrite(filename,cropped_img)
现在的问题是它工作正常,直到它获得像这样的文件名:tes_1.jpg ...但是当文件名较长时不起作用。请看一下:
(Env_CV2) C:\Users\mainu\Documents\ACADEMIC YEAR 20_21\Master thesis\Implementation\Applications\Apps_with_opencv>"c:/Users/mainu/Documents/ACADEMIC YEAR 20_21/Master thesis/Implementation/Applications/Apps_with_opencv/Env_CV2/Scripts/python.exe" "c:/Users/mainu/Documents/ACADEMIC YEAR 20_21/Master thesis/Implementation/Applications/Apps_with_opencv/Crop_app.py"
old name : test_1.jpg
new name : test_1.jpg
old name : test_2.jpg
new name : test_2.jpg
old name : test_3.jpg
new name : test_3.jpg
old name : test_4.jpg
new name : test_4.jpg
old name : test_5.jpg
new name : test_5.jpg
old name : WebCam_RGB_Car_2_screws_out_#1_°0-03_24_21-10_45.jpg
Traceback (most recent call last):
File "c:/Users/mainu/Documents/ACADEMIC YEAR 20_21/Master thesis/Implementation/Applications/Apps_with_opencv/Crop_app.py", line 13, in <module>
rows,colmn,channel = img.shape # to know the shape of the image
AttributeError: 'NoneType' object has no attribute 'shape'
如果有人能告诉我原因..?
错误显示您的 img 变量是 None
。这意味着行 cv2.imread(imgfile)
为您的 for 循环中的某个文件返回了 None
。
如果您传递的文件参数不存在,则默认使用 cv2.imread 方法 returns None
。它不会引发任何错误。
您可能想在那里放置一个 try-except 或 if-else 块来处理丢失的图像。
我想我找到了错误的原因。我的图像文件名包含“°”和 space 之类的字符。当我删除它们时它起作用了。
我正在尝试编写一个 python 脚本,该脚本应将图像作为输入并裁剪它们,并将裁剪后的图像保存在具有相同(原始)文件名的不同文件夹中。所以我写了下面的代码..
import cv2
#import numpy
import os
import glob
#import sys
for imgfile in glob.glob(r"C:\Users\mainu\Documents\ACADEMIC YEAR 20_21\Master thesis\Implementation\Applications\Apps_with_opencv\Test\*.jpg"):
img= cv2.imread(imgfile)
name = os.path.basename(imgfile)
print('old name :',name)
rows,colmn,channel = img.shape # to know the shape of the image
#cv2.imshow('This image has :' + str(rows) +' number of rows,' + str(colmn) +' number of columns',img) #shows the image
#cv2.waitKey(0) #waits untill any key is pressed
cropped_img = img[400:949,408:1588] # here we are cutting the image
#rows,colmn,channel = cropped_img.shape # to know the shape of the new image
#cv2.imshow('This image has :' + str(rows) +' number of rows,' + str(colmn) +' number of columns',cropped_img) #shows the new image
#cv2.waitKey(0) #waits untill any key is pressed
filename = r"C:\Users\mainu\Documents\ACADEMIC YEAR 20_21\Master thesis\Implementation\Applications\Apps_with_opencv\Test_2/" + name
name_2 = os.path.basename(filename)
print('new name :',name_2)
cv2.imwrite(filename,cropped_img)
现在的问题是它工作正常,直到它获得像这样的文件名:tes_1.jpg ...但是当文件名较长时不起作用。请看一下:
(Env_CV2) C:\Users\mainu\Documents\ACADEMIC YEAR 20_21\Master thesis\Implementation\Applications\Apps_with_opencv>"c:/Users/mainu/Documents/ACADEMIC YEAR 20_21/Master thesis/Implementation/Applications/Apps_with_opencv/Env_CV2/Scripts/python.exe" "c:/Users/mainu/Documents/ACADEMIC YEAR 20_21/Master thesis/Implementation/Applications/Apps_with_opencv/Crop_app.py"
old name : test_1.jpg
new name : test_1.jpg
old name : test_2.jpg
new name : test_2.jpg
old name : test_3.jpg
new name : test_3.jpg
old name : test_4.jpg
new name : test_4.jpg
old name : test_5.jpg
new name : test_5.jpg
old name : WebCam_RGB_Car_2_screws_out_#1_°0-03_24_21-10_45.jpg
Traceback (most recent call last):
File "c:/Users/mainu/Documents/ACADEMIC YEAR 20_21/Master thesis/Implementation/Applications/Apps_with_opencv/Crop_app.py", line 13, in <module>
rows,colmn,channel = img.shape # to know the shape of the image
AttributeError: 'NoneType' object has no attribute 'shape'
如果有人能告诉我原因..?
错误显示您的 img 变量是 None
。这意味着行 cv2.imread(imgfile)
为您的 for 循环中的某个文件返回了 None
。
如果您传递的文件参数不存在,则默认使用 cv2.imread 方法 returns None
。它不会引发任何错误。
您可能想在那里放置一个 try-except 或 if-else 块来处理丢失的图像。
我想我找到了错误的原因。我的图像文件名包含“°”和 space 之类的字符。当我删除它们时它起作用了。