2 回答

TA貢獻1890條經驗 獲得超9個贊
好吧,您可以通過查看以下源代碼輕松找到答案img_to_array:
def img_to_array(img, data_format='channels_last', dtype='float32'):
"""Converts a PIL Image instance to a Numpy array.
# Arguments
img: PIL Image instance.
data_format: Image data format,
either "channels_first" or "channels_last".
dtype: Dtype to use for the returned array.
# Returns
A 3D Numpy array.
# Raises
ValueError: if invalid `img` or `data_format` is passed.
"""
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Unknown data_format: %s' % data_format)
# Numpy array x has format (height, width, channel)
# or (channel, height, width)
# but original PIL image has format (width, height, channel)
x = np.asarray(img, dtype=dtype)
if len(x.shape) == 3:
if data_format == 'channels_first':
x = x.transpose(2, 0, 1)
elif len(x.shape) == 2:
if data_format == 'channels_first':
x = x.reshape((1, x.shape[0], x.shape[1]))
else:
x = x.reshape((x.shape[0], x.shape[1], 1))
else:
raise ValueError('Unsupported image shape: %s' % (x.shape,))
return x
因此,主要區別在于您可以將數據格式參數img_to_array傳遞給以將通道放置在第一個軸或最后一個軸上。此外,它將確保返回的數組是一個 3D 數組(例如,如果給定的輸入img是一個可能表示灰度圖像的 2D 數組,那么它將添加另一個維度為 1 的軸以使其成為 3D 數組)。
請注意,盡管在 docstring 中提到輸入圖像是 PIL 圖像實例,但它也適用于 numpy 數組甚至 Python 列表(因為輸入首先轉換為 numpy 數組:)x = np.asarray(img, dtype=dtype)。
添加回答
舉報