Como converter corretamente uma matriz 3d secreta em bytes rgb contíguos

eu estou tentandoconverter a matriz 3d retornada por cv2.imread na matriz de bytes rgb contígua, para exibir no GTK. E abaixo está o meu código de conversão:

def ndarr2rgb(img):
    r_arr = img[:, :, 0].ravel()    # read channel
    g_arr = img[:, :, 1].ravel()    # green channel
    b_arr = img[:, :, 2].ravel()    # blue channel
    numItems = img.shape[0] * img.shape[1] * img.shape[2]   # number of entries in byte array
    z = img.shape[2]                # z dimension, always equal to 3
    arr = np.zeros((numItems, 1))
    # to fill the byte array with r,g,b channel
    for i in xrange(0, numItems):
        if i % z == 0:
            arr[i] = r_arr[i/z]
        if i % z == 1:
            arr[i] = g_arr[i/z]
        if i % z == 2:
            arr[i] = b_arr[i/z]
    return arr

Portanto, no meu código, primeiro pego as três dimensões separadamente em r_arr, g_arr, b_arr, depois coloco os valores na ordem ou RGB no 'arr'. Portanto, após a iteração, o array 'arr' será como 'r0, g0, b0, r1, g1, b1, ...'

E então eu uso "GdkPixbuf.Pixbuf.new_from_data"função para obter o pixbuf do arr retornado pelo"ndarr2rgb"função acima. E eu uso"image.set_from_pixbuf"para exibir a imagem. Mas obtive o seguinte resultado:
 É como se houvesse alguma área barulhenta, então por favor me ajude a resolver meu problema, thx.

questionAnswers(4)

yourAnswerToTheQuestion