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 - OpenCV does not support transparency in the standard imread call โ€“ you need cv2.imread with a flag or a separate alpha channel merge for RGBA data. scikit-image is useful when you are already working in a scientific Python environment. The io.imread() function normalizes the output to float values in the [0, 1] range, which is convenient for machine learning pipelines. from skimage import io import numpy as np arr = io.imread('sample.png') print(type(arr)) print(arr.dtype) print(arr.shape) print(f"Value range: [{arr.min()}, {arr.max()}]")
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - Explanation: It converts a PIL image to a NumPy array using img_to_array() for processing, then back to a PIL image using array_to_img(), confirming smooth transformation with array type float32 and shape (200, 400, 3). For advanced, ...
Discussions

Python - byte image to NumPy array using OpenCV - Stack Overflow
Yes but it's the same code for ... docs.opencv.org/3.0-beta/modules/imgcodecs/doc/โ€ฆ ... @Norrius I read "I have image in bytes" and "image_bytes" and had to make the assumption it was. If that is not the case then OP needs to clarify in the question why it is different. ... @GPPK "byte image to numpy array using opencv" ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert image file object to numpy array in with openCv python? - Stack Overflow
I am developing API to upload Image using Flask. After uploading i want to modify image using openCV, As openCV requires numpy array object to i have file object, how do i convert to numpy array? H... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I turn numpy ndarray into an opencv image? Or alternatively, is there a module that can take numpy array screenshots at a high FPS?
Even cv2 creates ndarray! Pretty sure YOLO (or other ML model) uses ndarray. More on reddit.com
๐ŸŒ r/learnpython
2
2
August 9, 2022
python - How can I read a numpy array image with OpenCV? - Stack Overflow
You have a file that contains a lot of images, but what is the extension of this file? File or folder? A file also have a path.. ... If you have a npy file this file has a path because it is saved somewhere and you need numpy load. If it is instead an already generated array in your script than opencv can already handle it. You might have to ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Roboflow
roboflow.com โ€บ use opencv โ€บ convert pil image to numpy array
Convert PIL Image to NumPy array (OpenCV)
You can convert a PIL Image object into a NumPy array using the np.asarray() function: import numpy as np from PIL import Image pil_image = Image.open("image.jpeg") image = np.asarray(pil_image) OpenCV can be used with the open source supervision Python package.
๐ŸŒ
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 - Once you set up OpenCV as described in my previous post, go to your main.py file. 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), dtype=np.uint8) gray_image = np.ones((rows, columns), dtype=np.uint8) * 128 white_image = np.ones((rows, columns), dtype=np.uint8) * 255 # Write images to a file cv.imwrite("./images/black_image.jpg", black_image) cv.imwrite("./images/gray_image.jpg", gray_image) cv.imwrite("./images/white_image.jpg", white_image) # Renderโ€ฆ
๐ŸŒ
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 from...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 - It doesn't have its own internal storage format for images, instead, it uses NumPy arrays. The common scenario for using this library is when you need to convert an image from Pillow to NumPy so that you can work with it using OpenCV.
๐ŸŒ
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.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-opencv-read-image-cv2-imread
OpenCV Read Image - cv2 imread() - 3 Python Examples
To read an image in Python using OpenCV, use cv2.imread() function. imread() returns a numpy array containing values that represents pixel level data. You can read image as a grey scale, color image or image with transparency. Examples for all these scenarios have been provided in this tutorial.
๐ŸŒ
OpenCV
docs.opencv.org โ€บ 4.13.0 โ€บ d3 โ€บ df2 โ€บ tutorial_py_basic_ops.html
OpenCV: Basic Operations on Images
A good knowledge of Numpy is required to write better optimized code with OpenCV. ( Examples will be shown in a Python terminal, since most of them are just single lines of code ) ... Loads an image from a file. You can access a pixel value by its row and column coordinates. For BGR image, it returns an array ...
๐ŸŒ
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 ยท Numpy Opencv ยท
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
Image processing with Python, NumPy | note.nkmk.me
October 20, 2020 - By reading the image as a NumPy array ndarray, various image processing can be performed using NumPy functions. By operating ndarray, you can get and set (change) pixel values, trim images, concatenate images, etc. Those who are familiar with NumPy can do various image processing without using libraries such as OpenCV.
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

๐ŸŒ
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 - It serves as the groundwork for a variety of applications in computer vision, machine learning, and scientific imaging. Converting images into NumPy arrays requires a few core libraries. NumPy handles the array operations, while image libraries such as Pillow, OpenCV, Matplotlib, or scikit-image handle reading the image file into memory.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i turn numpy ndarray into an opencv image? or alternatively, is there a module that can take numpy array screenshots at a high fps?
r/learnpython on Reddit: How can I turn numpy ndarray into an opencv image? Or alternatively, is there a module that can take numpy array screenshots at a high FPS?
August 9, 2022 -

I'm attempting to automate a game using opencv and yolov5. My final problem is that I can't get a script to capture the screen fast enough to follow the objects correctly. I found a module called DXCam that can capture images at astounding speeds, though I can't use them in my algorithm as they are ndarrays and not normal arrays. Is there a way to convert the ndarrays into normal arrays (this process does not need to be fast, I can simply buffer them and alter them after they've been captured)? This is the script that produces the ndarrays:

def record():
    size = (pyautogui.size())
    box = pyautogui.locateOnScreen('game_screen.png', region=(0, 0, size[0], size[1]), grayscale=True, confidence=0.7)

    record_time = 3

    target_fps = 120
    writer = cv2.VideoWriter(
        "output.mp4", cv2.VideoWriter_fourcc(*"mp4v"), target_fps, (box.width, box.height)
    )
    camera = dxcam.create(output_color="RGB")
    camera.start(target_fps=120, video_mode=True, region=(box.left, box.top, box.left + box.width, box.top + box.height))
    cur_time = time.time()
    while time.time() < cur_time + record_time:
        writer.write(camera.get_latest_frame())
    camera.stop()
    writer.release()

Alternatively, are there any faster methods than this? This one for example is not fast enough for my needs (Merely 12-13 frames a second):

def record_bad():
    height = GetSystemMetrics(1)
    width = GetSystemMetrics(0)
    file_name = 'output.mp4v'
    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    captured_video = cv2.VideoWriter(file_name, fourcc, 120.0, (width, height))
    time_before = time.time()
    frames = []
    while time.time() > time_before + 3:
        img = ImageGrab.grab(bbox=(0, 0, width, height))  # bbox specifies specific region (bbox= x,y,width,height)
        img_final = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRGB)
        frames.append(img_final)
        cv2.waitKey(0)
    for frame in frames:
        captured_video.write(frame)
    captured_video.release()
    cv2.destroyAllWindows()
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 62602397 โ€บ how-can-i-read-a-numpy-array-image-with-opencv
python - How can I read a numpy array image with OpenCV? - Stack Overflow
import numpy as np import cv2 images = np.load("file.npy") for i, img_array in enumerate(images): if len(img_array.shape) == 2: img = img_array elif len(img_array.shape) == 3: img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) gray_img = ...