Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether my image is RGB format or BGR format in python? How do i convert them and viceversa?

I am doing some preprocessing things on pretrained data in OpenVino model. It says it only uses the BGR format image.

Here , How do i check in python whether my image is in BGR format or RBG format?

my loaded image code is as

import cv2
import numpy as np
from PIL import Image

image = cv2.imread('29101878_988024658021087_5045014614769664000_o.jpg')
print(image.shape)

Gives output of shape (973,772,3)

How do i check image is RBG or BGR format? If it is in RBG format How do i convert it to BGR and viceversa?


2 Answers

When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.

Reference : Note: In the case of color images, the decoded images will have the channels stored in B G R order.

Link : https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#imread)

To convert you can use

rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

and vice versa.

like image 150
Deepak Patankar Avatar answered Oct 20 '25 06:10

Deepak Patankar


To check if the image is in RGB or BGR format we can use:

import cv2
from PIL import Image

image = cv2.imread('image path')
img = Image.fromarray(image)
img.mode
like image 22
Spoorthi P Avatar answered Oct 20 '25 05:10

Spoorthi P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!