I'm trying to write a script which will take all of the images in a given directory and format the size.
I have been able to import the files as a list using OS and split them using the loop. I've printed each file name and index successfully, but when I try to get the dimensional values using cv2.imread(), it returns 'None', making it impossible for me to get the shape, and throws me an AttributeError.
I have already tried uninstalling and reinstalling opencv-python. As suggested here.
import os
import cv2
def imageResize():
dirlist = os.listdir('images')
for c, file in enumerate(dirlist):
print(c, file)
img = cv2.imread(file)
height, width = img.shape[0, 2]
print(height, width)
if __name__ == '__main__':
imageResize()
Expected Output:
0 image17_10.png
600px 400px
1 image15_9.png
500px 500px
...
Actual Output:
0 image17_10.png
height, width = img.shape[0,2]
AttributeError: 'NoneType' object has no attribute 'shape'
Welcome on stack overflow! It seems that you missed the path when opening the file, the line should be:
cv2.imread('images' + os.path.sep + file)
# or
cv2.imread(os.path.join('images', file))
or any other way of constructing the path to the file from the filename.