PIL rotate image colors (BGR -> RGB)

后端 未结 11 2194
深忆病人
深忆病人 2020-11-29 21:21

I have an image where the colors are BGR. How can I transform my PIL image to swap the B and R elements of each pixel in an efficient manner?

11条回答
  •  日久生厌
    2020-11-29 22:02

    Using the ideas explained before... using numpy you could.

    bgr_image_array = numpy.asarray(bgr_image)
    B, G, R = bgr_image_array.T
    rgb_image_array = np.array((R, G, B)).T
    rgb_image = Image.fromarray(rgb_image_array, mode='RGB')
    

    Additionally it can remove the Alpha channel.

    assert bgra_image_array.shape == (image_height, image_width, 4)
    B, G, R, _ = bgra_image_array.T
    rgb_image_array = np.array((R, G, B)).T
    

提交回复
热议问题