PIL.Image convert to numpy array

當使用PIL.Image讀取圖像時,若是直接使用numpy.array()轉換會出現錯誤:app

lst = list()
for file_name in os.listdir(dir_image):
    image = PIL.Image.open(file_name)
    lst.append(image)
arr = numpy.array(lst)

此時,上述最後一行在執行時會出現錯誤:code

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image'

解決辦法以下:對象

lst = list()
for file_name in os.listdir(dir_image):
    image = PIL.Image.open(file_name)
    lst.append(np.array(image))
arr = numpy.array(lst)

即,在list中的元素都已轉化爲numpy.array,而非直接的Image對象。string

相關文章
相關標籤/搜索