1. 程式人生 > 其它 >python#AttributeError: ‘NoneType‘ object has no attribute ‘astype‘ 麻煩檢查路徑

python#AttributeError: ‘NoneType‘ object has no attribute ‘astype‘ 麻煩檢查路徑

技術標籤:Py小Bugpython

測試處理圖片報錯

AttributeError: ‘NoneType’ object has no attribute ‘astype’
在這裡插入圖片描述

尋找解決方案1

參考此篇博文,然後可能是下載下來的照片的檔案字尾名和檔案本身的屬性不匹配,所以又重新處理照片一遍
https://blog.csdn.net/weixin_43826242/article/details/90325955


#Attribute Error: 'NoneType' object has no attribute 'astype'
#將所有的圖片轉換成為jpg格式(防止因為圖片格式造成的cv2.
imread()異常) #所有的照片雖然都是.jpg格式的,但是有的照片沒有進行轉碼,只是更改了字尾,因此在模型載入的時候才會報錯,可以使用PIL工具將所有的jpg圖片轉換成為jpg圖片,從而避免這樣的麻煩 #參考博文https://blog.csdn.net/weixin_43826242/article/details/90325955 import PIL.Image as Image import os path='D:/test_img' def start(Path): filelist = os.listdir(Path) for file in filelist: img =
Image.open(Path + '/'+file).convert('RGB') print(img) print(file) img.save('D:/test_pic/'+file) print('Done!') start(path)

然鵝還是報錯,風暴哭泣!!!

尋找解決方案2

參考此篇博文,修改了路徑
https://blog.csdn.net/junyilao/article/details/83502050

原來是:


for allDir in pathDir:  # 遍歷資料夾
    path = r'D:\test_img'
+ allDir pic_name = allDir # 圖片名稱 image=cv2.imread(path)

修改後

下面展示一些 內聯程式碼片


for allDir in pathDir:  # 遍歷資料夾
    path = r'D:\test_img\\' + allDir
    pic_name = allDir  # 圖片名稱
    image=cv2.imread(path)


綜上,就是多了一個斜槓\
我的豬腦子到底值幾兩呦!!!