Python OpenCV: Convert Image BGR and RGB Without cv2.cvtColor()

We can use cv2.cvtColor() to convert image brg to rgb in python opencv. Here is the tutorial:

Python OpenCV: Convert Image BGR and RGB Using cv2.cvtColor()

However, we also can do not use cv2.cvtColor() to convert. In this tutorial, we will introduce you how to do.

Python OpenCV - Convert Image BGR and RGB Without cv2.cvtColor()

1.Open an image using python opencv

import cv2
import numpy as np
from PIL import Image

im_cv = cv2.imread('data/src/lena.jpg')

2.Convert image brg to rgb

im_rgb = im_bgr[:, :, [2, 1, 0]]

3.Use python pillow to save converted image

Image.fromarray(im_rgb).save('data/dst/lena_rgb_pillow.jpg')

You should notice: you can not use cv2.imwrite() to save image.

Run this code, you will see:

Python OpenCV: Convert Image BGR and RGB Using cv2.cvtColor()