import numpy as np
from random import sample
def foo(M,N):
#Create an np array of size M X N
img = np.zeros((M,N))
#Create a colour set within grayscale
colours = set(range(255))
#Iterate through the entire image pixel
for i in range(M):
for j in range(N):
#img[i-1:i+2,j - 1:j + 2].flatten() : neighbouring pixel
#including the current pixel
#colours- set(img[i-1:i+2,j - 1:j + 2].flatten() : Colours not in the neighbouring pixel
#sample(colours- set(img[i-1:i+2,j - 1:j + 2].flatten()),1)[0]: Select one from the above
#and assign to the current pixel
img[i,j] = sample(colours- set(img[i-1:i+2,j - 1:j + 2].flatten()),1)[0]
return img
>>> foo(10,10)
array([[ 153., 128., 15., 163., 180., 189., 186., 228., 65.,
140.],
[ 52., 229., 220., 54., 79., 105., 11., 146., 29.,
70.],
[ 244., 119., 188., 147., 230., 157., 28., 243., 105.,
62.],
[ 188., 135., 129., 144., 192., 11., 90., 193., 35.,
149.],
[ 20., 130., 140., 134., 191., 63., 50., 180., 49.,
4.],
[ 88., 175., 254., 151., 176., 30., 122., 157., 88.,
82.],
[ 37., 190., 10., 187., 221., 83., 2., 115., 191.,
148.],
[ 53., 70., 150., 127., 168., 141., 179., 65., 253.,
59.],
[ 96., 3., 225., 218., 87., 76., 41., 195., 221.,
192.],
[ 28., 35., 104., 130., 207., 57., 204., 228., 96.,
174.]])