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
Discussions

python - how to load a color image file as a numpy array? - Stack Overflow
Beware that cv2.imread() returns a numpy array in BGR not RGB. 2017-01-25T20:55:34.88Z+00:00 ... OpenCV seem to have dropped the mode argument. See my answer below for an updated method. 2019-03-09T20:57:50.873Z+00:00 ... PIL (Python Imaging Library) and Numpy work well together. More on stackoverflow.com
🌐 stackoverflow.com
python - Converting Numpy Array to OpenCV Array - Stack Overflow
I'm trying to convert a 2D Numpy array, representing a black-and-white image, into a 3-channel OpenCV array (i.e. an RGB image). Based on code samples and the docs I'm attempting to do this via Py... More on stackoverflow.com
🌐 stackoverflow.com
numpy - Creating an RGB picture in Python with OpenCV from a randomized array - Stack Overflow
I want to create a RGB image made from a random array of pixel values in Python with OpenCV/Numpy setup. I'm able to create a Gray image - which looks amazingly live; with this code: import nump... More on stackoverflow.com
🌐 stackoverflow.com
How to convert RGB array to image?
After reading your comments, Quoted from this comment Nevermind, i fixed the code with chatgtp: *ChatGPT, also see rule #5 Then this comment im using it anyways because it works https://imgur.com/a/42M2LbK Finally this comment How would that produce the image I want? Just run it first? I've already solved it with chatGTP. *ChatGPT, a crappy solution Your solution doesn't make any sense Your manner is bad (you don't even try it or thank u/Strict-Simple ). More on reddit.com
🌐 r/learnpython
16
0
May 6, 2023
🌐
YouTube
youtube.com › mixible
How to convert a python numpy array to an RGB image with Opencv 2 4 - YouTube
Welcome to Mixible, your go-to source for comprehensive and informative content covering a broad range of topics from Stack Exchange websites. Our channel is...
Published: October 28, 2023
Views: 15
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert a python numpy array to an rgb image
5 Best Ways to Convert a Python NumPy Array to an RGB Image - Be on the Right Side of Change
February 20, 2024 - To convert a NumPy array to an RGB image with OpenCV, you use the cv2.imwrite() function, which saves an array as an image file directly.
🌐
Iditect
iditect.com › faq › python › how-to-convert-a-python-numpy-array-to-an-rgb-image-with-opencv.html
How to convert a python numpy array to an RGB image with Opencv?
In this example, we first create a NumPy array rgb_array that simulates an RGB image with random pixel values. You should replace this with your own image data or processing logic. We then use cv2.cvtColor() to convert the NumPy array from RGB to BGR format, which is the format used by OpenCV.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python numpy howtos" › "how to save numpy array as image in python"
How to Save NumPy Array as Image in Python | Delft Stack
February 2, 2024 - Here, image_data is a 3x3 NumPy array representing a grayscale image. The cv2.imwrite() function in OpenCV is simple to use and allows you to save a NumPy array as an image in various formats.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - For advanced, high-performance ... extra conversions. Unlike PIL and Keras, it loads images in BGR format, so converting to RGB with cv2.cvtColor() is essential for tasks like classification or segmentation....
🌐
GitHub
gist.github.com › walkoncross › 9e2074a0efc2710302be49ec668b5249
Numpy / OpenCV image BGR to RGB · GitHub
Conversion between any/all of BGR, RGB, and GBR may be necessary when working with Matplotlib expects M x N x 3 image, where last dimension is RGB. OpenCV expects M x N x 3 image, where last dimension is BGR.
🌐
SciVision
scivision.dev › numpy-image-bgr-to-rgb
Numpy / OpenCV image BGR to RGB | Scientific Computing
April 27, 2022 - Matplotlib pyplot.imshow(): M x N x 3 image, where last dimension is RGB. OpenCV imshow(): M x N x 3 image, where last dimension is BGR · Scientific Cameras: some output M X N x 3 image, where last dimension is GBR · Note: as in any programming language, operations on memory-contiguous arrays are most efficient.
🌐
Reddit
reddit.com › r/learnpython › how to convert rgb array to image?
r/learnpython on Reddit: How to convert RGB array to image?
May 6, 2023 -

So I tried to convert a raw RGB array to an image with this simple code:

import numpy as np

from PIL import Image

Canvas = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 0], [0, 0, 0]])

img = Image.fromarray(Canvas, 'RGB')

img = img.save("canvas.png")

I have no idea how to set the size of the image as a resolution of 2x2 because this code does not create the correct resolution

The first pixel in Canvas should be red, second pixel should be green, the third and fourth pixels should be black

All I want is a 2x2 resolution image where the top-left pixel is red and the top-right pixel is green, then both bottom pixels should be black, but my code is just producing the wrong image resolution with the wrong pixel locations. What is the issue?

Top answer
1 of 2
4

In your comment you specify that the red_arr, etc. are arrays of the range -4000 to 4000.

But if we take a look at the specifications of the Image.from_array modes, then we see that it expects a matrix of three bytes (values from zero to 255).

This is however not per se a problem: we can perform:

def rescale(arr):
    arr_min = arr.min()
    arr_max = arr.max()
    return (arr - arr_min) / (arr_max - arr_min)

red_arr_b = 255.0 * rescale(red_arr)
green_arr_b = 255.0 * rescale(green_arr)
blue_arr_b = 255.0 * rescale(blue_arr)

arr[:,:,0] = red_arr_b
arr[:,:,1] = green_arr_b
arr[:,:,2] = blue_arr_b

img = Image.fromarray(arr.astype(int), 'RGB')

So first we rescale to a range 0 to 255, and then we feed that array to the PIL.

It is also possible that we want to scale red, green and blue the same way. In that case we can use:

def rescale(arr):
    arr_min = arr.min()
    arr_max = arr.max()
    return (arr - arr_min) / (arr_max - arr_min)

arr[:,:,0] = red_arr
arr[:,:,1] = green_arr
arr[:,:,2] = blue_arr

arr = 255.0 * rescale(arr)

img = Image.fromarray(arr.astype(int), 'RGB')
2 of 2
4

Your np array should have data type as uint8:

arr = np.zeros((len(x), len(z), 3),dtype='uint8')

Essentially, PIL calls tobytes function on each strides of your arr and if they are of type int, extra bytes will be generated.

Here are the result of following piece of code with different data type.

import numpy as np
from PIL import Image

size=241

arr = np.zeros((size,size,3))

arr[:,:,0] = [[255]*size]*size
arr[:,:,1] = [[255]*size]*size
arr[:,:,2] = [[0]*size]*size

img = Image.fromarray(arr.astype(int), 'RGB')

import numpy as np
from PIL import Image

size=241

arr = np.zeros((size,size,3))

arr[:,:,0] = [[255]*size]*size
arr[:,:,1] = [[255]*size]*size
arr[:,:,2] = [[0]*size]*size

img = Image.fromarray(arr.astype('uint8'), 'RGB')

As suggested by others, if your original input are not in range (0,255), you will need to rescale them as well.

🌐
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.
🌐
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 - import cv2 import numpy as np # ... image cv2.imwrite('output.png', array) In this code, we first create a 100x200 Numpy array with three channels (for RGB)....
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-numpy-array-to-an-image
Convert a NumPy array to an image - GeeksforGeeks
July 15, 2025 - OpenCV is another popular library for computer vision tasks. It can also convert a NumPy array into an image using its functions like cv2.imshow() for displaying or cv2.imwrite() for saving the array as an image file.
🌐
Towards Data Science
towardsdatascience.com › home › artificial intelligence › deep learning › how rgb and grayscale images are represented in numpy arrays?
How RGB and Grayscale Images Are Represented in NumPy Arrays? | Towards Data Science
December 4, 2021 - A batch of 3 RGB images can be represented using a four-dimensional (4D) NumPy array or a tensor. Here, we need an extra dimension to represent the number of images. ... Channels-last: This places the color channel axis at the end.