Posterize Greyscale image

I would like to upload a greyscale image. Then read each pixel value and change it..

Any help?

You could start here:

but there are other methods available, probably using ML or similar

Thanks very much. I'll give it a try, adapted to my needs.