python 画像処理でエラーが出ます。直し方を教えてください。
pythonで、ファイル内のすべての画像サイズを整えて保存したいのですが、エラーが出ます。
どうすればいいでしょうか?
↓ソースコード
import os
import cv2
import glob
def resize_rectangle_img(from_dir,to_dir,size=None):
#file_type = 'jpg'
#img_list = glob.glob('./' + from_dir + '/*.' + file_type)
files = os.listdir(from_dir)
i = 0
for file in files:
img = cv2.imread(from_dir+file,1)
#img.show()
# Get the height and width of the loaded image
height= img.shape[0]
width = img.shape[1]
x1 = y1 = 0
x2 = width
y2 = height
diff = abs(height - width)
if height > width:
y1 = int(diff/2)
y2 = height - y1
elif width>height:
x1 = int(diff/2)
x2 = width - x1
img = img[y1:y2,x1:x2]
if size!=None:
img = cv2.resize(img,(size,size))
cv2.imwrite(to_dir+file,img)
print(str(i))
print("\n")
i = i+1
if __name__ == '__main__':
resize_rectangle_img('images/original/','images/resized/')
print("=====Resized!=====")
エラー内容↓
File "resize.py", line 18, in resize_rectangle_img
height= img.shape[0]
AttributeError: 'NoneType' object has no attribute 'shape'
回答よろしくお願いします。
0 likes