PIL rotate image colors (BGR -> RGB)

后端 未结 11 2200
深忆病人
深忆病人 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:08

    Just to add a more up to date answer:

    With the new cv2 interface images loaded are now numpy arrays automatically.
    But openCV cv2.imread() loads images as BGR while numpy.imread() loads them as RGB.

    The easiest way to convert is to use openCV cvtColor.

    import cv2
    srcBGR = cv2.imread("sample.png")
    destRGB = cv2.cvtColor(srcBGR, cv2.COLOR_BGR2RGB)
    

提交回复
热议问题