Reputation: 37
I'm trying to scale and save a thousand images to a directory.
I succeeded to resize images. However, errors occur while saving.
Code is below. Help me pls.
import cv2
import numpy as np
import os
def scaling_shirink(addr):
img = cv2.imread(addr)
height, width = img.shape[:2]
shrink = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
cv2.imshow('Shrink', shrink)
cv2.waitKey(0)
cv2.destroyAllWindows()
count = 0
IMAGE_DIR_BASE = 'C:/ClassShared\Data/CM_ML_IMG_181011/CASE_01/FPS_10_PNG'
image_file_list = os.listdir(IMAGE_DIR_BASE)
for file_name in image_file_list:
image = scaling_shirink(IMAGE_DIR_BASE + '/' + file_name)
cv2.imwrite('C:/ClassShared\Data/CM_ML_IMG_181011/CASE_01/34_sdetect_db1/' + '_' + "%04d" % (count) + '.png', image)
count = count + 1
Error messages are as follows.
Traceback (most recent call last):
File "C:/PycharmProjects/TS_S/Scailing.py", line 19, in <module>
image = scaling_shirink(IMAGE_DIR_BASE + '/' + file_name)
File "C:/PycharmProjects/TS_S/Scailing.py", line 8, in scaling_shirink
height, width = img.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
I don't understand why it says AttributeError: 'NoneType' object has no attribute 'shape'
Upvotes: 2
Views: 4960
Reputation: 236
EDIT:
Check if the image path is correct and if it its in fact a image with the formats accepted by Opencv. Because if your path is wrong the img = cv2.imread(addr)
will return None
and height, width = img.shape[:2]
will throw an error
Also, your function scaling_shirink() is returning None. To fix it, just change it to the function below:
def scaling_shirink(addr):
img = cv2.imread(addr)
height, width = img.shape[:2]
shrink = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
cv2.imshow('Shrink', shrink)
cv2.waitKey(0)
cv2.destroyAllWindows()
#this return was missing
return shrink
That should work!
Upvotes: 1