Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a grey image in python?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I got:
a coloured image

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading