You don't need to convert NumPy array to Mat because OpenCV cv2 module can accept NumPyarray. The only thing you need to care for is that {0,1} is mapped to {0,255} and any value bigger than 1 in NumPy array is equal to 255. So you should divide by 255 in your code, as shown below.

img = numpy.zeros([5,5,3])

img[:,:,0] = numpy.ones([5,5])*64/255.0
img[:,:,1] = numpy.ones([5,5])*128/255.0
img[:,:,2] = numpy.ones([5,5])*192/255.0

cv2.imwrite('color_img.jpg', img)
cv2.imshow("image", img)
cv2.waitKey()
Answer from bikz05 on Stack Overflow
🌐
Indian AI Production
indianaiproduction.com › home › blog › explained cv2.imshow() function in detail | show image
Explained cv2.imshow() function in Detail | Show image
January 13, 2021 - Syntax: cv2.imshow( windows_name, numpy_array) windows_name: string value numpy_array: 2d or 3d NumPy array
Discussions

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
I have a problem which I am trying to solve. I have a file.npy containing a lot of images. Each image is a numpy array inside that matrix file. so what I want to do is to apply some cv2 functions o... More on stackoverflow.com
🌐 stackoverflow.com
python - Access numpy array in openCV - Raspberry Pi Stack Exchange
On the last line of that script, image is a numpy array with shape (rows, cols, color-plane) with the color planes in BGR order - which is precisely how OpenCV represents image data. In other words, you can just pass that array straight to OpenCV functions: import time import picamera import picamera.array import cv2 with picamera.PiCamera() as camera: time.sleep(2) with picamera.array... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
August 4, 2015
How to combine cv2 video frames and numpy audio array using ffmpeg to give hls playlist as output?

Which os? Windows or linux?

More on reddit.com
🌐 r/ffmpeg
3
3
April 22, 2024
🌐
Note.nkmk.me
note.nkmk.me › home › python › opencv
Reading and saving image files with Python, OpenCV (imread, imwrite) | note.nkmk.me
October 15, 2022 - In Python and OpenCV, you can read (load) and write (save) image files with cv2.imread() and cv2.imwrite(). Images are read as NumPy array ndarray. This article describes the following contents. Read ...
🌐
OpenCV
docs.opencv.org › 3.4.20 › d3 › df2 › tutorial_py_basic_ops.html
OpenCV: Basic Operations on Images
>>> import numpy as np · >>> import cv2 as cv · >>> img = cv.imread('messi5.jpg') >>> assert img is not None, "file could not be read, check with os.path.exists()" You can access a pixel value by its row and column coordinates. For BGR image, it returns an array of Blue, Green, Red values.
🌐
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.
🌐
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()
🌐
Saturn Cloud
saturncloud.io › blog › converting-numpy-arrays-to-images-using-cv2-and-pil
Converting Numpy Arrays to Images using CV2 and PIL | Saturn Cloud Blog
May 1, 2026 - In this code, we first create a 100x200 Numpy array with three channels (for RGB). We then fill the left half of the array with orange and the right half with blue. Finally, we use cv2.imwrite() to save the array as an image.
Find elsewhere
🌐
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 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) _, thr_img = cv2.threshold(gray_img, 0, 255, cv2.THRESH_OTSU) cv2.imshow(f"Thresholded Image {i}", thr_img) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python numpy array to cv2 mat
5 Best Ways to Convert Python Numpy Array to cv2 Mat - Be on the Right Side of Change
February 20, 2024 - If numpy array represents raw image data, you can use cv2.imdecode() to interpret the array as image data and return a Mat object. This is particularly useful when reading from network sources or encryption/decryption where the data may be in ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-opencv-cv2-imread-method
cv2.imread() method - Python OpenCV - GeeksforGeeks
July 12, 2025 - The cv2.imread() function return a NumPy array if the image is loaded successfully. ... In this example we are reading the image as a color image.
🌐
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 - UpdatedMarch 11, 2021•1 min read•View as Markdown · C · Clementine Pages · 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 ·
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 175684 › video-frame-to-numpy-array
Video frame to numpy array - OpenCV Q&A Forum
In CV2 everything is returned as NumPy objects: What is different between all these OpenCV Python interfaces? ... import cv2 cap = cv2.VideoCapture('rain.avi') cv2.namedWindow('frame',cv2.WINDOW_NORMAL) cv2.resizeWindow('frame', 800,800) while(cap.isOpened()): ret, frame = cap.read() #np_frame = cv2.imread('video', frame) # does not work #np_frame = np.asarray(cv2.GetMat(frame)) # does not work #print(np_frame.shape) print(frame.shape) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
🌐
Medium
anjalisingh-ratan.medium.com › image-manipulation-and-processing-using-opencv-and-numpy-d4e5404f3ddd
Image Manipulation and processing using OpenCV and NumPy | by Anjali Singh | Medium
June 12, 2021 - ... The color format in general is RGB(Red, Blue, Green) but the color code format used in OpenCV is BGR(Blue, Green, Red). ... To read an image, we use .imread() function of the cv2 module, specify the path of image in argument for the 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
images_array = sitk.GetArrayFromImage(images) print(np.amin(images_array),np.amax(images_array)) print(images_array[24].shape, images_array[24].dtype) ... 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.
🌐
Medium
rcvaram.medium.com › images-are-just-numpy-arrays-ceb7b0307fcf
Images are JUST NumPy Arrays. How computes can read the images , How… | by Sivaram Rasathurai | Medium
December 9, 2024 - When we read a colour image by OpenCV as follow · import opencv img = cv2.imread("ImagePath") print(img) If we execute the above code, we will get a huge NumPy array. Colour image is a collection of three parallel planes, and every plane represent the colour intensity of a single channel.
🌐
AskPython
askpython.com › python › examples › convert-images-to-numpy-arrays
How to Convert Images to Numpy Arrays in Python - AskPython
April 21, 2026 - OpenCV is faster than Pillow and ... trips up many people coming from other libraries. import cv2 import numpy as np arr = cv2.imread('sample.png') ......
🌐
Jmgphd
jmgphd.com › courses › csc5930 › lecture-notes › opencv-images
Opening and Displaying Images with OpenCV – Jason M. Grant
Images are stored in NumPy arrays, so all NumPy array operations are available when using the OpenCV library. OpenCV supports several options for reading in images, though we will make use of two extensively in this class. All list of all options are shown here. ... Next, we will call the function cv2.imread.
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Importing Image Data Into NumPy Arrays | Pluralsight
April 15, 2025 - 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 from BGR to RGB.
🌐
Scaler
scaler.com › home › topics › opencv cv2.imread()
OpenCV cv2.imread() method - Scaler Topics
May 26, 2023 - The flags parameter in the cv2.imread() method is important because it allows you to specify how the image file should be read and processed. The cv2.imread() method in OpenCV returns a NumPy array representing the image read from the file.