image.blob
generally refers to a Blob (Binary Large Object) where image data is stored in binary format. To convert image.blob
into a numpy.array
in Python, you need to decode the binary data into an image first, and then convert it into an array. The most common methods are using Pillow or OpenCV.
from PIL import Image
import numpy as np
import io
img_blob = image.blob # bytes
# onvert to Pillow
img = Image.open(io.BytesIO(img_blob))
# convert to np array
img_array = np.array(img)
import numpy as np
import cv2
img_blob = image.blob # bytes
# convert to numpy array decode using OpenCV
nparr = np.frombuffer(img_blob, np.uint8)
img_array = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # or IMREAD_UNCHANGED
Considerations
Image.open
or cv2.imdecode
may fail. The image must be in a standard format like JPEG or PNG.image.blob
is truly a blob, or if it’s actually something else like a file path or a base64 string.