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)。
添加回答
举报