How to Convert image from PIL to OpenCV format in Python

Here we will discuss how to convert an image from PIL to OpenCV format using Python. PIL and OpenCV both are Python libraries mostly used in image processing. These are used to work with images.

While doing a project or working with images in python, you may need to convert images from PIL to OpenCV format to work faster. As OpenCV is faster than PIL.

To know the basics about OpenCV and PIL, you may go through with these links below:
Basics of OpenCV in Python
Basics of PIL in Python

Installing OpenCV and PIL

If your system does not have packages OpenCV or PIL, you need to install them. Before installing OpenCV, check if you have PIL in Python.
To install PIL, use the command below:

pip install Pillow

To install OpenCV, use the command below:

pip install opencv-python

 

Convert image from PIL to OpenCV format in Python

Images reading from the PIL library gives error while operating in OpenCV. As OpenCV follows BGR color format and PIL follows RGB color format.
Besides these, PIL uses integer division and on the other side, OpenCV uses float point percentages. So, we need to convert the PIL image into OpenCV format before processing further.

At First, we will import all the packages i.e. cv2(OpenCV), PIL, numpy. And open the image using PIL. Then converting the image into a numpy array. And finally, convert that numpy array into an OpenCV image.

We can solve it in two different ways. The first approach, by using the OpenCV color conversion command. The second approach, by using backward iteration conversion in color format.

Using Color Conversion Code

cv2.COLOR_RGB2BGR converts the arrangement of Red-Green-Blue color into Blue-Green-Red color format.

import cv2
import numpy as np
from PIL import Image
pim = Image.open("testimg.png")
nimg = np.array(pim)
ocvim = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
fnam = "opencv image.png"
cv2.imwrite(fnam, ocvim)

Here ‘pim’ variable stores the image. The ‘nimg’ variable stores the conversion of the image in a numpy array. And ‘ocvim’ stores the color converted image.
Now to save the output image, we have used the cv2.imwrite() function.

 

Using backward iteration conversion in color format

Convert image using reverse iteration conversion technique.

import cv2
import numpy as np
from PIL import Image
pim = Image.open("testimg.png").convert('RGB')
nimg = np.array(pim)
ocvim = ocvim[:, :, ::-1].copy() 
fnam = "opencv image.png"
cv2.imwrite(fnam, ocvim)

Here ‘pim’ stores the opened image. ‘ocvim’ stores the image of color conversion from the numpy array. And the rest are already explained in the above section.

For both cases, the input image and output image look the same. It does not have any changes except the color format.

Input:

How to Convert image from PIL to OpenCV format in Python

 

Output:

How to Convert image from PIL to OpenCV format in Python

Hope you find it useful!
Thanks for your time.

Leave a Reply

Your email address will not be published. Required fields are marked *