我正在尝试从文本文件中读取图像。文本文件包含这些图像的路径。图像在不同的目录中,我检查了它们是否存在。
PATH_IN = 'D:\user\data\Augmentation'
path_out = 'D:\user\data\Augmentation\images_90t'
try:
if not os.path.exists('images_90t'):
os.makedirs('images_90t')
except OSError:
print ('Error: Creating directory of data')
with open('filelist.txt', 'r') as f:
for image_path in f.readlines():
image = cv2.imread(image_path, 1)
print("The type of image is: " , type(image)) # OUTPUT: The type of image is: <class 'NoneType'>
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, 90, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
#cv2.imshow("rotated", rotated)
cv2.imwrite(path_out, rotated)
cv2.waitKey(0)
我在1和2中寻找答案,但没有解决方案。大多数情况下,人们建议将编辑为
\
或类似的内容,因为通往图像的路径可能是错误的。我认为我已经尝试了所有组合,但仍然没有解决方案。该错误在行 (h, w) = image.shape[:2]
中引起的说法
AttributeError: 'NoneType' object has no attribute 'shape'
我想通往cv2.imread()
的路径不能以图像打开它,而是给出 nontype 对象。这是我的文本文件中的一些示例:
D:userdata16_partitions_annotatedpartition1images 73-1 73-1_00311.jpg
D:userdataImageNet_Utils-masterimagesn03343560_url2077528821_231f057b3f.jpg
D:userdatalighterimageswebcam-fire3scene00211.jpg
D:userdatasmoke11imagesscene07341.jpeg
D:userdatasmoke11imagesscene07351.jpeg
我在 Windows 7,64。
有人可以帮忙吗?谢谢。
使用读取线时,您会获得lineFeed/newline字符。如果您做
print(repr(image_path))
您将在输出中看到新线( n(。使用strip((删除空格(空格,标签,新线,返回马车(的开始和结束。因此您的代码变为:
import os
import cv2
PATH_IN = 'D:\user\data\Augmentation'
path_out = 'D:\user\data\Augmentation\images_90t'
try:
if not os.path.exists('images_90t'):
os.makedirs('images_90t')
except OSError:
print ('Error: Creating directory of data')
with open('filelist.txt', 'r') as f:
for image_path in f.readlines():
print(repr(image_path)) # will show the newlines n in image_path
image_path = image_path.strip()
image = cv2.imread(image_path)
print("The type of image is: " , type(image)) # OUTPUT: The type of image is: <class 'NoneType'>
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, 90, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
#cv2.imshow("rotated", rotated)
path_out = os.path.join(path_out, os.path.basename(image_path))
cv2.imwrite(path_out, rotated)
cv2.waitKey(0)
我还修复了您的path_out
分配,以将所有输出文件放在正确的位置。