如何将所有颜色生成单个像素?

时间:2019-07-08 04:04:31

标签: python numpy opencv

我可以通过堆叠3个np.zeros([1,1,1])来生成所有黑色像素

但是为什么不呢?

import numpy as np
import cv2

m  = np.zeros([8,32,1])

k = 0 
for i in range(m.shape[0]):
    for j in range(m.shape[1]):
        for z in range(m.shape[2]):
            m[i,j,z] = k
            k+=1
a = np.stack(m,m,m)

cv2.imwrite('rgb.png',a)

错误

axis = normalize_axis_index(axis, result_ndim)
TypeError: only size-1 arrays can be converted to Python scalars

1 个答案:

答案 0 :(得分:0)

好的,因此我对您的代码做了一些修改以显示其工作原理:

m  = np.zeros([8,32])

k = 0 
for i in range(m.shape[0]):
    for j in range(m.shape[1]):
        m[i,j] = k
        k+=1
a = np.stack((m,m,m), axis=-1)

输出:

enter image description here