You don't necessarily have to convert the image to normalised floats. You have it in the correct shape (MxNx3) and in the correct range (0-255), but it must be of type np.uint8. From the documentation

X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)

Display the image in X to current axes. X may be a float array, a uint8 array or a PIL image. If X is an array, it can have the following shapes:

MxN – luminance (grayscale, float array only)
MxNx3 – RGB (float or uint8 array)
MxNx4 – RGBA (float or uint8 array)

However, your array data is in BGR format, so you need to reverse the third dimension to bring it to RGB format.

image = np.array(M, dtype=np.uint8)[...,::-1]

Another aspect that caught my attention is your definition of the data. The list of lists represents the rows first, so M actually has red/green in the first row and blue/black in the second. If you want the pixels in the order you stated them, i.e., red/blue and green/black, you have to transpose the rows with the columns.

image_transp = np.transpose(image, (1, 0, 2))

Now you can plot whichever image representation is the correct one (image or image_transp). For the latter, you get

plt.imshow(image_transp, interpolation='none')

Answer from Reti43 on Stack Overflow
🌐
Pythoninformer
pythoninformer.com › python-libraries › numpy › numpy-and-images
PythonInformer - Image processing with pillow and NumPy
October 2, 2022 - Now let's fill the array with orange pixels (red=255, green=128, blue=0). We use slices to do this, the three values are broadcast across all the rows and columns of the array: ... The PIL function Image.fromarray function creates a PIL image from a NumPy array.
🌐
Reddit
reddit.com › r/learnpython › converting between pil image and 2d pixel array?
r/learnpython on Reddit: Converting between PIL Image and 2d pixel array?
May 11, 2022 -

I need to do the following:

  1. Import an image.

  2. Convert image to a 2D array of pixel values (rgb tuples, bytes, whatever it doesn't matter).

  3. Manipulate that 2D array (needs to be 2D as I'm using a library that requires a 2D array of data).

  4. Convert the newly changed 2D array back into an image.

  5. Save the image.

Anyone know how I can accomplish this? PIL Image documentation has some functions to get data but it's all 1D arrays, and I'm not sure how to get them into 2D arrays and then re-convert it back into an image again.

Discussions

how to convert array of pixel values to image for displaying using python and opencv,numpy - Stack Overflow
I am trying to convert the numpy array consisting of pixel values of a image to display the image. More on stackoverflow.com
🌐 stackoverflow.com
January 22, 2019
python - How to create image from a list of pixel values in Python3? - Stack Overflow
If I have a list of pixel rows from an image in the following format, how would get the image? [ [(54, 54, 54), (232, 23, 93), (71, 71, 71), (168, 167, 167)], [(204, 82, 122), (54, 54, 54), ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to convert an image into an array of its raw pixel data? - Stack Overflow
I wanted to create a small pixel-by-pixel image manipulation program, so I wanted to ask if there is something (preferably in Python) that can convert a .png image into RGB raw pixel data. For exam... More on stackoverflow.com
🌐 stackoverflow.com
Generate pixel image in Python from array - Stack Overflow
Python 2.6: Creating image from array seems to be doing something different · I thought for a simple example, I could create an image from scratch that varies in pixel intensity. I use the A_pixelIntensity array to declare the intensities of pixel colors. More on stackoverflow.com
🌐 stackoverflow.com
January 13, 2016
Top answer
1 of 3
5

You don't necessarily have to convert the image to normalised floats. You have it in the correct shape (MxNx3) and in the correct range (0-255), but it must be of type np.uint8. From the documentation

X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)

Display the image in X to current axes. X may be a float array, a uint8 array or a PIL image. If X is an array, it can have the following shapes:

MxN – luminance (grayscale, float array only)
MxNx3 – RGB (float or uint8 array)
MxNx4 – RGBA (float or uint8 array)

However, your array data is in BGR format, so you need to reverse the third dimension to bring it to RGB format.

image = np.array(M, dtype=np.uint8)[...,::-1]

Another aspect that caught my attention is your definition of the data. The list of lists represents the rows first, so M actually has red/green in the first row and blue/black in the second. If you want the pixels in the order you stated them, i.e., red/blue and green/black, you have to transpose the rows with the columns.

image_transp = np.transpose(image, (1, 0, 2))

Now you can plot whichever image representation is the correct one (image or image_transp). For the latter, you get

plt.imshow(image_transp, interpolation='none')

2 of 3
4

The values of your RGB array must represent the desired color; the easiest is maybe to convert RGB values to floats in [0, 1]: (convert [0, 255] --> [0, 1])

import numpy
import matplotlib.pyplot as plt
randim_image1 = numpy.asarray([[(0., 0., 1.), (0., 1., 0.)], [(1., 0., 0.), (0., 0., 0.)]])

plt.imshow(image1, interpolation="none")

🌐
Python Programming
pythonprogramming.net › python-pixel-arrays
Python Programming Tutorials
Here is a 3-dimensional array of the data. All of the data is the image, each matrix block is a row of data, and each element within that is the pixel values in RGB-A (Red Green Blue Alpha).
🌐
Quora
quora.com › How-do-I-convert-a-matrix-of-pixels-to-an-image-in-Python
How to convert a matrix of pixels to an image in Python - Quora
Answer: I would use the Python Imaging Library, or some of it’s more modern implementations, like PILLOW. (PIL comes pre-installed with some versions of python and can be easily installed with PIP for other versions and installations) Here is a example that saves a image of rows alternating ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - The code then prints the image’s type, format (PNG), color mode (RGB) and dimensions (width, height). Example 2: In this example, we use the Keras API functions img_to_array() and array_to_img() to convert a PIL image to a NumPy array and then convert it back to an image.
🌐
Stack Overflow
stackoverflow.com › questions › 34756642 › generate-pixel-image-in-python-from-array
Generate pixel image in Python from array - Stack Overflow
January 13, 2016 - A_pixelIntensity = np.asarray([[1,1,1,1,1],[1,2,2,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,1,1,1,1]]) plt.imshow(A_pixelIntensity) ... Sign up to request clarification or add additional context in comments.
🌐
Stack Overflow
stackoverflow.com › questions › 63597440 › how-do-i-directly-turn-an-array-of-pixels-into-an-image-with-pil-in-python
How do I directly turn an array of pixels into an image with PIL in python? - Stack Overflow
Anyone know what it could be that I need to use? I would really appreciate the help, and I will be backing up my code next time! 🦄 ... Save this answer. ... Show activity on this post. That's a really great question past me. Let me answer that for you. I found this finally. You are wanting the 'putdata()' function. Add these lines after changing the val list: Copyim2 = Image.new(im.mode, im.size) im2.putdata(val) im2.save('new.png')
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Importing Image Data Into NumPy Arrays | Pluralsight
April 15, 2025 - We will use the pyplot class from the Matplotlib library to plot the image into the frame. # load and display an image with Matplotlib from matplotlib import image from matplotlib import pyplot # load image as pixel array image = image.imre...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
Image processing with Python, NumPy | note.nkmk.me
October 20, 2020 - By the operation for each element (= pixel) of the array, two images can be alpha-blended or composited based on a mask image. See the following articles for details. Alpha blending and masking of images with Python, OpenCV, NumPy
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-numpy-array-to-an-image
Convert a NumPy array to an image - GeeksforGeeks
July 15, 2025 - ... import numpy as np from PIL import Image a = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # Convert `a` to a PIL Image img = Image.fromarray(a) img.show() ... Explanation: np.random.randint() generates random integers (0-255) ...
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › arrays_and_images.html
Arrays as images, images as arrays — Functional MRI methods
A grayscale image is an array containing numbers giving the pixel intensity values - in our case between 0 and 99. Here we set gray to the default colormap for the rest of our plots: >>> # Set 'gray' as the default colormap >>> plt.rcParams['image.cmap'] = 'gray' We can also plot lines in matplotlib. For example, we might want to plot the values in row 8 from this array. Because Python indices start at 0, this is the 9th row of the array.
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.3.0 documentation
A lookup table, containing 256 (or 65536 if self.mode==”I” and mode == “L”) values per band in the image. A function can be used instead, it should take a single argument. The function is called once for each possible pixel value, and the resulting table is applied to all bands of the image.
🌐
Guiwitz
guiwitz.github.io › PyImageCourse_beginner › 02-Images_as_arrays.html
2. Images as arrays — Image processing with Python for beginners
Again the operation happens element-wise and because of this we have to ensure that the two arrays have the same dimensions. If they don’t Numpy doesn’t know what to do with pixels that don’t have an equivalent in the other array. As we are studying image processing, let’s simulate noise in our image.
🌐
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?

🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-109.php
Python NumPy: Convert a numpy array to an image - w3resource
August 29, 2025 - Python Code: # Importing the Image module from the PIL library from PIL import Image # Importing the NumPy library and aliasing it as 'np' import numpy as np # Defining image width and height img_w, img_h = 200, 200 # Creating a NumPy array 'data' filled with zeros of shape (img_h, img_w, 3) representing RGB channels data = np.zeros((img_h, img_w, 3), dtype=np.uint8) # Setting a specific pixel (100, 100) in the 'data' array to the color red [255, 0, 0] data[100, 100] = [255, 0, 0] # Creating an image object 'img' from the NumPy array 'data', specifying the color mode as 'RGB' img = Image.fromarray(data, 'RGB') # Saving the image as 'test.png' img.save('test.png') # Displaying the image img.show() Sample Output: Explanation: In the above code – ·