1
寫一些代碼要經過200個圖像的文件夾來獲得像素的最小數量的值的圖像(即最小寬度和高度)蟒蛇打開目錄錯誤
但我不斷收到此錯誤:
File "pixelSizeCheck.py", line 9, in get_num_pixels
width, height = Image.open(open(filepath)).size
IsADirectoryError: [Errno 21] Is a directory:
當我運行這段代碼:
from PIL import Image
import os.path
def get_num_pixels(filepath):
heightMin= 10000
widthMin= 10000
for filename in os.listdir(filepath):
if filename.endswith(".jpg") :
width, height = Image.open(open(filepath)).size
if width< widthMin:
widthMin = width
return widthMin
if height < heightMin:
heightMin = height
return heightMin
print (heightMin, widthMin)
get_num_pixels("filepath")
但是我已經測試代碼print語句,我知道我已經把該目錄是正確的,因爲它達到在for循環正常,但隨後崩潰當我嘗試並獲得第9行:
Image.open(open(filepath)).size
也是我比較新的蟒蛇等如何提高代碼的任何建議,將不勝感激。如果任何代碼不嚴格或者不好,請提前道歉。
如果我使用的glob我不斷收到這個奇怪的錯誤文件「pixelSizeCheck.py」,10號線,在get_num_pixels width,height = Image.open(f).size 文件「/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py」,第2419行,in打開 prefix = fp.read(16) 文件「/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/codecs.py」,第321行,解碼爲 (result,consume)= self ._buffer_decode(data,self.errors,final) UnicodeDecod eError:'utf-8'編解碼器無法解碼位置0中的字節0xff:無效起始字節 –
抱歉,還有另一個問題:您必須以二進制格式打開文件。 –
如何打開二進制文件?對不起,新的python –