obsidian09
obsidian09

Reputation: 11

How to create a grey image in python?

I tried to create a grey 3x3 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 3x3 image

What I got: a coloured image

Upvotes: 0

Views: 122

Answers (1)

Barış Aktaş
Barış Aktaş

Reputation: 426

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")

Upvotes: 1

Related Questions