编辑:从我的评论复制以提供更多问题的上下文
我忘了提及的一件重要事情是我的最终目标是添加 作为熊猫数据框的一列。我在尝试时遇到错误 由于不止一个,将像素数组按原样放入数据帧 尺寸。
我有一个32x32的图像,我想将其展平为一维numpy数组,该数组表示该像素处具有RGB值的对象。
img = Image.open(img_path)
pixels = np.asarray(img)
width, height = img.size
pixels = pixels.reshape(width * height, 3)
目前,这是我能做的最好的事情,而不会丢失一个对象中的RGB值分组。但是,通过这种实现,我得到了一个二维数组,每个元素都是这样的RGB值数组。
shape: (1024, 3)
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
I would like my array to have shape (1024,1)
and for each element to be some object (maybe a tuple?) of RGB values. Thanks.
c.f.注释,数组作为数据框或一系列:
Then you can use
column
as a column of another dataframe, if you so wish