read an image and convert it to bgr color format in python

To read an image and convert it to BGR color format in Python, you can use the OpenCV library. Here's an example code snippet:

main.py
import cv2

# load image in grayscale
img_gray = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# convert to BGR color format
img_bgr = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)
179 chars
8 lines

In the code above, we first load the image into a grayscale format using the cv2.imread() function. This function takes the filename of the image as input, as well as a flag indicating the format in which to read the image (in this case, grayscale).

Next, we use the cv2.cvtColor() function to convert the grayscale image to BGR color format. This function takes the input image and a flag indicating the conversion type as input (in this case, cv2.COLOR_GRAY2BGR indicates that we want to convert from grayscale to BGR).

The resulting img_bgr variable will contain the image data in BGR color format, which you can then use for further image processing tasks.

gistlibby LogSnag