Histogram contrast stretch

Hi Folks
Does anyone have a script to calculate the histogram of an image and then perform a linear contrast stretch using either pixel values or percentage clips values.

At present using a per pixel min/max stretch but this is not the usual way to enhance imagery.

Grateful of any ideas. My javascript experience is limited (but growing) any python non-exsitent.

You can do a statistical API request first to find out the min/max/historgram values
Then use stretch function.

Some further ideas can be found here:

If you want to go into more scientific details, read this paper: