How do six pixels become a two-row image?

reshape refactors the dimensions while preserving the element count. The default traversal uses C index order.

numpy.reshape in practice

File pixels numbered zero through five three to a row and the second row begins at three. Shape compatibility alone cannot tell you the original width. Read it from image metadata.

import numpy as np
np.reshape(np.arange(6), (2, 3))

Result [[0, 1, 2], [3, 4, 5]]

中文

一串像素怎样还原成两行?

reshape 重新划分轴,元素总数必须保持一致。默认按 C 索引顺序读取和放回数值。

numpy.reshape 算例

把编号 0 到 5 的像素每三个装一行,第二行就从 3 开始。能重排并不代表知道原图的宽度,宽度应由元数据给出。

import numpy as np
np.reshape(np.arange(6), (2, 3))

结果 [[0, 1, 2], [3, 4, 5]]

References / 参考来源

Keep reading / 继续阅读