In order to prepare learning data for image recognition in Python, I am writing a program that uses OpenCV to clip and save all images in each of the following folders into squares to fit the shorter sides.
data/arisu
/aya
/karen
/shino
/youko
However, if you do this, you may not be able to locate the file or get an error.
height, width=img.shape[:2]
AttributeError: 'NoneType' object has no attribute' shape'
I've never written a for statement with a file folder, so I'm not very good at it, but please let me know if anyone knows.
importos
import cv2
dirs=['arisu', 'aya', 'karen', 'shino', 'youko']
for i in range (len(dirs)) :
d=dirs[i]
files=os.listdir("../data/"+d)
for jin range (len(files)) :
f=files[j]
img = cv2.imread("../data/"+d+"/"+f)
height,width=img.shape [:2]
if(height>width): #Vertical length
clp = img [0:width, 0:width] # Clip from top left to width square
if(height<width): # If it was horizontal
offset=(width//2)-(height//2))# offset the left margin
clp=img [0:height, offset:offset+height] #Clip from offset position to square height length
cv2.imwrite("../data/"+d+"/"+f,clp)
After cv2.imread, I changed the specification to if img!=None:
and returned to the beginning of the for statement by only printing ("failure") when it was None, and it worked correctly.
I edited this post based on @3614013s' Comment and posted it as Community Wiki.I edited this post based on @3614013s' Comment and posted it as Community Wiki.
© 2024 OneMinuteCode. All rights reserved.