Just do

Image.fromarray(np.array(img).astype(np.uint8).transpose(1,2,0))
Answer from Maiia Bocharova on Stack Overflow
๐ŸŒ
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?

Discussions

python - Import a RGB array into a PIL image with fromarray - Stack Overflow
But the output image is Red Black Black Black instead of Red Green Blue Black. ... NB: Not a duplicate of convert RGB arrays to PIL image. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Combine 3 separate numpy arrays to an RGB image in Python - Stack Overflow
Now I need to combine them to form an RGB image. I tried 'Image' to do the job but it requires 'mode' to be attributed. I tried to do a trick. I would use Image.fromarray() to take the array to image but it attains 'F' mode by default when Image.merge requires 'L' mode images to merge. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - PIL: Image.fromarray(img.astype('uint8'), mode='RGB') returns grayscale image - Stack Overflow
I have converted a pytorch tensor of size torch.Size([3, 28, 28]) to a numpy array of size (28, 28, 3), and there doesn't seem to be any problems with that. I then try to convert this to a PIL image More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Converting from numpy arrays to a RGB image - Stack Overflow
I have three (241, 241) numpy arrays which I would like to treat as the Red, Green and Blue components of an image. I have tried this: import numpy as np from PIL import Image arr = np.zeros((le... More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 2, 2018
๐ŸŒ
Pythoninformer
pythoninformer.com โ€บ python-libraries โ€บ numpy โ€บ numpy-and-images
PythonInformer - Image processing with pillow and NumPy
October 2, 2022 - The PIL function Image.fromarray function creates a PIL image from a NumPy array. If the NumPy array has the shape (height, width, 3) it will automatically create an RGB image. We can then use the PIL function save to save the image.
๐ŸŒ
Readthedocs
pc-pillow.readthedocs.io โ€บ en โ€บ latest โ€บ Image_class โ€บ Image_fromarray.html
3. Image fromarray โ€” PC-Pillow
from PIL import Image import numpy as np w, h = 256, 256 data = np.full(shape=(h, w, 3), fill_value=[135, 206, 250], dtype=np.uint8) im = Image.fromarray(data, 'RGB') # im.show() im.save("Image/Image_fromarray_LightSkyBlue.png") The code below creates a numpy array of size 256x256 with background in black.
๐ŸŒ
Pillow Documentation
pillow.readthedocs.io โ€บ en โ€บ stable โ€บ reference โ€บ Image.html
Image module - Pillow (PIL Fork) 12.3.0 documentation
When creating RGB or HSV images, you can also use color strings as supported by the ImageColor module. See Colors for more information. If the color is None, the image is not initialised. ... An Image object. PIL.Image.fromarray(obj: SupportsArrayInterface, mode: str | None = None) โ†’ Image[source]ยถ
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:pillow โ€บ image module โ€บ fromarray()
Python:Pillow | Image Module | fromarray() | Codecademy
June 8, 2024 - Each color has an 8-bit pixel to store its value, which means CMYK has 4x8-bit pixels to store the values. RGB: It represents the color palette made from Red, Green, and Blue colors.
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.

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
Image processing with Python, NumPy | note.nkmk.me
October 20, 2020 - pil_img = Image.fromarray(im) print(pil_img.mode) # RGB pil_img.save('data/temp/lena_save_pillow.jpg') source: pillow_ndarray_io.py ยท A grayscale image (2D array) can also be passed to Image.fromarray(). mode automatically becomes 'L' (grayscale). It can be saved with save().
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-109.php
Python NumPy: Convert a numpy array to an image - w3resource
August 29, 2025 - # 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()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - Explanation: It loads 'Sample.png' and converts it to a NumPy array using asarray(), then prints the raw RGB pixel values row by row, showing the image's numerical structure. Example 4: In this we use Image.fromarray() to revert back from NumPy array to image format.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-a-numpy-array-to-an-image
Convert a NumPy array to an image - GeeksforGeeks
July 15, 2025 - The array is created with np.uint8 for image compatibility. Image.fromarray(a) converts the array to a Pillow Image and img.show() displays the randomly generated 100x100 RGB image.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72161115 โ€บ rgb-array-to-pil-image
python - RGB array to PIL image - Stack Overflow
May 8, 2022 - Copyim_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size))), mode='RGB')
๐ŸŒ
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 from BGR to RGB.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ opencv
Convert BGR and RGB with Python, OpenCV (cvtColor) | note.nkmk.me
May 14, 2019 - When performing image processing with Pillow, you can convert ndarray to a PIL.Image object with Image.fromarray(), but in Pillow the color order assumes RGB (red, green, blue).