使用readlines时,会得到换行符/换行符。如果你做了
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
将所有输出文件放置在正确位置的赋值。