将 uint32 的元组转换为 uint8 的 3d numpy 数组的最佳方法

Best way of converting tuple of uint32's to 3d numpy array of uint8's

我从外部 api 获得图像,显示为 uint32 元组,其中每个 uint32 由 4 个 uint8 组成,带有 r、g、b 和 alpha(始终 = 0),我需要将其转换为3d numpy 数组,其格式与我可以从 imageio.imread 获得的格式相同。问题是当我使用 numpy.view 时,颜色的顺序是颠倒的。这是我写的代码,工作正常,但我想知道是否有更好的转换方式。

        frame_buffer_tuple = ... # here we have tuple of width*height length
        framebuffer = np.asarray(frame_buffer_tuple).astype(dtype='uint32').view(dtype='uint8').reshape((height, width, 4)) # here I have height x width x 4 numpy array but with inverted colors (red instead of blue) and alpha I don't need
        framebuffer = np.stack((framebuffer[:, :, 2], framebuffer[:, :, 1], framebuffer[:, :, 0]), axis=2) # here I've got correct numpy height x width x 3 array

我更关心执行时间,然后是内存,但由于我可以有 7680 × 4320 张图像,所以两者可能都很重要。谢谢!

是否只需要反转最后一个维度?尝试

framebuffer = framebuffer [:, :, :2:-1] # drops alpha

@RichieV 为我指出了正确的方向,实际上起作用的是:

framebuffer = np.asarray(frame_buffer_tuple).astype('uint32').view('uint8').reshape((height, width, 4))[:, :, 2::-1]

我暂时将此标记为解决方案,直到有人想出解决此问题的解决方案为止。