python - 创建具有固定颜色的三 channel 图像的最佳方法?

标签 python numpy scipy

我想创建一个尺寸为 10x5 且颜色固定为 [0, 1, 2] 的 numpy 三 channel 图像。我目前正在使用以下代码进行操作:

x = np.array([0, 1, 2])
x = np.array((x,) * 10)
x = np.array((x,) * 5)

这可行,但不是很优雅。用更少的代码实现相同目标的最好/最有效的方法是什么?

最佳答案

或者,您可以使用 np.full:

np.full((10, 5, 3), [0, 1, 2])

它创建一个给定形状的数组 (10, 5, 3) 并用常量值 [0, 1, 2] 填充它。

关于python - 创建具有固定颜色的三 channel 图像的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51932766/

相关文章:

python - 如何在没有相位跳跃的情况下连接正弦波

python - sin(y) 导致 "can' t 将表达式转换为 float"错误

python - 在 Python 的 matplotlib 中调整各个子图的高度

Python 到 MATLAB : exporting list of strings using scipy. io

python - 如何解决sympy中的 "Absolute values cannot be inverted in the complex domain"错误

python - 在新终端中运行辅助脚本

python - 如何防止 Visual Studio Code Python 调试器在处理异常时停止?

python - 如何在单次观察中运行 pykalman 卡尔曼滤波器? (Python)

Python - 如何使用其他 numpy 数组正确索引 numpy 数组,类似于 MATLAB

python - 寻找稀疏矩阵中的最大特征值