sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Threshold upper and lower limit

Open LachlanNewman opened this issue 3 years ago • 2 comments

Question about an existing feature

Is there an existing way to implement the threshold function but with a lower and upper limit? eg. threshold(lowerLimit,upperLimit)

What are you trying to achieve?

I want to make it so that all pixels above the upper limit will be converted to 255 and all pixels below the lower limit will be converted to 0 but the pixels that lie between the limits will remain unchanged.

LachlanNewman avatar Feb 22 '23 03:02 LachlanNewman

Hi, did you find a solution? This sounds like the sort of thing a look up table (LUT) would solve. There's an open issue to track this future possible feature at https://github.com/lovell/sharp/issues/1441

In the meantime, you could output raw, uncompressed pixel data and write your own loop to modify pixel values. https://sharp.pixelplumbing.com/api-output#raw

Alternatively, you might be able to hand-roll an ICC colour profile with a "curve" that matches the required behaviour, but this would probably require a bit of effort.

lovell avatar Feb 27 '23 10:02 lovell

Let's continue to track this at #1441

lovell avatar Apr 27 '23 10:04 lovell