I’m new to cv2, I know that when I need to load an image from a file i use:
img = cv2.imread(IMG_PATH)
but I already have the image in binary format from the database.
how can I get the same result but from binary not from file path?
The following code demonstrates how to do it. Since
cv2 expects images to be represented in the BGR color channel order, you have to swap the red and blue channel in a typical RGB image (can be done using
import requests import cv2 import numpy as np # just to get a sample image img = requests.get('https://unsplash.it/id/767/500/300').content # bytestring img_buf = np.asarray(bytearray(img), dtype='uint8') cv2_img = cv2.imdecode(img_buf, cv2.IMREAD_UNCHANGED) cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
Answered By – asdf