HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelTutorial Source codeReport an issue

Linear stretch image

← localContrastImage   magnifyImage →
Stretches with saturation the image intensity.
Discards any pixels below the black point and above the white point and levels the remaining pixels. The calculation is done by number of pixels, rather than absolute color values.

In the code example below using a value of 0.10 for both the black and white threshold, would move the darkest 10% of pixels to black, the lightest 10% of pixels to white, and level the remaining pixels.

A value of zero for either black or white point means that that end of the histogram is not adjusted.
function linearStretchImage($image_path, $blackThreshold, $whiteThreshold)
    $imagick = new \Imagick(realpath($image_path));
    $pixels = $imagick->getImageWidth() * $imagick->getImageHeight();
    // TODO - I'm really not sure if the scaling is meant to be done
    // by number of pixels or by ::getQuantum()
//        $blackThreshold * $pixels,
//        $whiteThreshold * $pixels
        $blackThreshold * Imagick::getQuantum(),
        $whiteThreshold * Imagick::getQuantum()

    header("Content-Type: image/jpeg");
    echo $imagick->getImageBlob();