You can use newer OpenCV python interface (if I'm not mistaken it is available since OpenCV 2.2). It natively uses numpy arrays:

import cv2
im = cv2.imread("abc.tiff")
print(type(im))

result:

<type 'numpy.ndarray'>
Answer from Andrey Kamaev on Stack Overflow
🌐
AskPython
askpython.com › python › examples › convert-images-to-numpy-arrays
How to Convert Images to Numpy Arrays in Python - AskPython
April 21, 2026 - It reads images as BGR by default, which trips up many people coming from other libraries. import cv2 import numpy as np arr = cv2.imread('sample.png') print(type(arr)) print(arr.dtype) print(arr.shape) print(f"Value range: [{arr.min()}, ...
Discussions

Python - byte image to NumPy array using OpenCV - Stack Overflow
I have an image in bytes: print(image_bytes) b'\xff\xd8\xff\xfe\x00\x10Lavc57.64.101\x00\xff\xdb\x00C\x00\x08\x04\x04\x04\x04\x04\x05\x05\x05\x05\x05\x05\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x... More on stackoverflow.com
🌐 stackoverflow.com
python - How to convert images into numpy array quickly? - Stack Overflow
To train the image classification model I'm loading input data as NumPy array, I deal with thousands of images. Currently, I'm looping through each image and converting it into a NumPy array as shown More on stackoverflow.com
🌐 stackoverflow.com
February 8, 2022
Any way to convert opencv images to format expected by textures?
Hi something like that ? https://www.youtube.com/watch?v=-JZK8h-3bNk&t More on reddit.com
🌐 r/DearPyGui
9
2
January 14, 2022
How to convert numpy image array (output from a DL model) to base64 and sent to front end html
How to handle image storage is a tricky topic, it depends on the scale, usage, and speed you want to achieve. If this is for a small scale application or a pet project I suggest saving those images to the file system and read it through Jinja, as opposed to converting it to base64, blob, storing into a db. More on reddit.com
🌐 r/flask
6
2
April 17, 2021
🌐
Clementine Blog
clementine.hashnode.dev › how-to-read-image-from-path-and-convert-to-a-numpy-array-in-python
How to read Image from Path and Convert to a NumPy Array in Python.
March 11, 2021 - Three methods to convert an image path to a NumPy array: Use OpenCV · # OpenCV import cv2 img = cv2.imread(image_path) # reads an image in the BGR format img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR -> RGB · Use Matplotlib · # Use Matplotlib ...
🌐
Cloudinary
cloudinary.com › home › convert image to numpy array: methods, tips, and examples
Convert Image to NumPy Array: Methods, Tips, and Examples | Cloudinary
January 24, 2026 - Because OpenCV loads images directly as NumPy arrays, unlike Pillow, it is well-suited for high-performance pipelines and large-scale image processing. The cv2.imread() function reads the file and returns a NumPy array ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - Explanation: Loads 'Sample.png' using OpenCV’s cv2.imread() (BGR format), converts it to RGB with cv2.cvtColor() and saves it using cv2.imwrite(). Confirms OpenCV treats images as NumPy arrays by default.
🌐
Roboflow
roboflow.com › use opencv › convert pil image to numpy array
Convert PIL Image to NumPy array (OpenCV)
import numpy as np from PIL import Image pil_image = Image.open("image.jpeg") image = np.asarray(pil_image)
🌐
Medium
manivannan-ai.medium.com › drawing-image-using-numpy-and-opencv-565abdbb3670
Drawing Image using Numpy and Opencv | by Manivannan Murugavel | Medium
December 14, 2017 - We will create numpy array · import cv2 import numpy as npcolor_image = np.zeros((512,512,3),np.unit8) bw_image = np.zeros((512,512),np.unit8) cv2.imshow("Color Image",color_image) cv2.imshow("BW Image",bw_image) cv2.waitKey(0) cv2.destroyAllWindows() Draw Image Numpy ·
Find elsewhere
🌐
Aniruddha Sadhukhan
anianiruddha.wordpress.com › 2018 › 01 › 17 › python-convert-images-to-numpy-arrays-using-opencv-and-vice-versa
Python : convert Images to Numpy Arrays using OpenCV and vice versa – Aniruddha Sadhukhan
January 17, 2018 - """ Program to convert Images to Numpy Arrays using OpenCV and vice versa by Aniruddha """ import cv2 im_g = cv2.imread("smallgray.png",0) #0 = grayscale print(im_g) #*** # [[187 158 104 121 143] # [198 125 255 255 147] # [209 134 255 97 182]] #*** im_bgr = cv2.imread("smallgray.png",1) #1 = bgr print(im_bgr) #*** # [[[187 187 187] # [158 158 158] # [104 104 104] # [121 121 121] # [143 143 143]] # [[198 198 198] # [125 125 125] # [255 255 255] # [255 255 255] # [147 147 147]] # [[209 209 209] # [134 134 134] # [255 255 255] # [ 97 97 97] # [182 182 182]]] #*** cv2.imwrite("newimg.png",im_g) pr
🌐
GitHub
gist.github.com › b4f7bed3c4c059a765c89315f522db27
Converting and saving images to numpy array. · GitHub
Converting and saving images to numpy array. GitHub Gist: instantly share code, notes, and snippets.
🌐
Medium
medium.com › @carlosdnotes › create-numpy-images-with-opencv-4b512d5993e4
Create NumPy Images with OpenCV. Create, display, and save images with… | by CarlosD | Medium
October 27, 2025 - Replace everything inside that file with the following code: #!/usr/bin/env python3 import cv2 as cv import numpy as np def main(): # Set image size, in pixels rows = 6 columns = 8 # Create images black_image = np.ones((rows, columns), ...
Top answer
1 of 2
47

I created a 2x2 JPEG image to test this. The image has white, red, green and purple pixels. I used cv2.imdecode and numpy.frombuffer

import cv2
import numpy as np

f = open('image.jpg', 'rb')
image_bytes = f.read()  # b'\xff\xd8\xff\xe0\x00\x10...'

decoded = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1)

print('OpenCV:\n', decoded)

# your Pillow code
import io
from PIL import Image
image = np.array(Image.open(io.BytesIO(image_bytes))) 
print('PIL:\n', image)

This seems to work, although the channel order is BGR and not RGB as in PIL.Image. There are probably some flags you might use to tune this. Test results:

OpenCV:
 [[[255 254 255]
  [  0   0 254]]

 [[  1 255   0]
  [254   0 255]]]
PIL:
 [[[255 254 255]
  [254   0   0]]

 [[  0 255   1]
  [255   0 254]]]
2 of 2
1

I searched all over the internet finally I solved:

NumPy array (cv2 image) - Convert

NumPy to bytes

and

bytes to NumPy

:.

#data = cv2 image array
def encodeImage(data):
    #resize inserted image
    data= cv2.resize(data, (480,270))
    # run a color convert:
    data= cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
    return bytes(data) #encode Numpay to Bytes string



def decodeImage(data):
    #Gives us 1d array
    decoded = np.fromstring(data, dtype=np.uint8)
    #We have to convert it into (270, 480,3) in order to see as an image
    decoded = decoded.reshape((270, 480,3))
    return decoded;

# Load an color image
image= cv2.imread('messi5.jpg',1)

img_code = encodeImage(image) #Output: b'\xff\xd8\xff\xe0\x00\x10...';
img = decodeImage(img_code) #Output: normal array
cv2.imshow('image_deirvlon',img);
print(decoded.shape)

You can get full code from here

🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Importing Image Data Into NumPy Arrays | Pluralsight
April 15, 2025 - To work with an OpenCV library, you need to install it in the virtual environment using pip install opencv-contrib-python. The cv2 package provides an imread() function to load the image. It also reads a PIL image in the NumPy array format. The only thing we need to convert is the image color ...
🌐
YouTube
youtube.com › watch
OpenCV Numpy Basics - Image Data Structure - Raspberry Pi - YouTube
Learn how images are structured for processing in OpenCV and learn the very basics of Numpy for use in Open CV. After this video, you will understand how Num...
Published: October 2, 2024
🌐
Python Examples
pythonexamples.org › python-opencv-read-image-cv2-imread
OpenCV Read Image - cv2 imread() - 3 Python Examples
You can read image into a numpy array using opencv library. The array contains pixel level data. And as per the requirement, you may modify the data of the image at a pixel level by updating the array values. To read an image in Python using OpenCV, use cv2.imread() function.
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 225478 › convert-a-2d-numpy-array-to-cv_8uc1-type
Convert a 2D Numpy array to CV_8UC1 type - OpenCV Q&A Forum
And then, I convert them into a Numpy array: images_array = sitk.GetArrayFromImage(images) print(np.amin(images_array),np.amax(images_array)) print(images_array[24].shape, images_array[24].dtype) With this output: 0.0 2380.6191 (240, 240) float32 · I have found that I can convert it to CV_8UC1 OpenCV type, but I don't know how. I want to convert it because I want to use it with the cv2.equalizeHist method.
🌐
GitHub
github.com › foolmarks › images_to_npy
GitHub - foolmarks/images_to_npy: Converting images to numpy files · GitHub
Each image should be read with the OpenCV imread function which converts an image file (JPEG, PNG, etc) into a numpy array.
Starred by 28 users
Forked by 10 users
Languages: Python
🌐
Uploadcare
uploadcare.com › processing & transformations category › fast pillow image import to numpy and opencv arrays
Fast Pillow image import to NumPy and OpenCV arrays | Uploadcare
September 7, 2021 - Here are the two most common ways to convert a Pillow image to NumPy. If you Google it, you’ll probably find one of them: numpy.array(im) — makes a copy from an image to a NumPy array.
🌐
Javatpoint
javatpoint.com › how-to-convert-images-to-numpy-array
How to Convert Images to NumPy Array - Javatpoint
How to Convert Images to NumPy Array with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.