Prev

[Python] Convert image bytes(image.blob) into numpy.array

@ 2025.04.29 05:14:52

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.

1. Using Pillow + numpy

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)

2. Using OpenCV + numpy

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