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

In this tutorial, we will introduce how to convert Image BGR to RGB using cv2.cvtColor() in python opencv.

Notice: Python opencv will open an image with brg mode.

Understanding Read an Image to Numpy Array with Python cv2.imread()

Python OpenCV - Convert Image BGR and RGB Using cv2.cvtColor() - a Step Guide

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 using cv2.cvtColor()

im_rgb = cv2.cvtColor(im_cv, cv2.COLOR_BGR2RGB)

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()