@katsuki5080

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

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

1Answer

cv2.imreadは、エラーしても例外を投げず、Noneを返すようです。
print (from_dir + file)して、確認してみてはいかがでしょうか。

0Like

Comments

  1. @katsuki5080

    Questioner

    確認したところ、ファイル内に.mat形式のものが紛れていたみたいでそれが邪魔していることがわかりました。
    アドバイスありがとうございます!

Your answer might help someone💌