How to create a grey image in python?

Question:

I tried to create a grey 3×3 pixel image in python, however the result is always a black image with several coloured pixels.

What I tried:

import numpy as np
from PIL import Image

greyimg = np.array([[[128]*3]*3]*3)
print(greyimg)
Image.fromarray(greyimg, 'RGB').save("test_grey.png")

What I expected:
a grey 3×3 image

What I got:
a coloured image

Asked By: obsidian09

||

Answers:

import numpy as np
from PIL import Image
import cv2

greyimg = np.array([[[128]*3]*3]*3,dtype=np.uint8)
print(greyimg)
Image.fromarray(greyimg, 'RGB').save("test_grey.png")
Answered By: Barış Aktaş