Sharp: Replace Color with transparency

Created on 5 Apr 2019  路  4Comments  路  Source: lovell/sharp

What are you trying to achieve?

I have a known rgb value that surrounds the screenshot I am taking, and would like to replace it with a white, fully transparent background. I have not seen examples of how to do this.

Have you searched for similar questions?

Yes. It seems most people want to take a transparent background and provide it with a color. I am looking to take a known color and change it, as well as make it transparent.

Are you able to provide a standalone code sample that demonstrates this question?

const trimWhiteSpaceAndPad = (path) => {
    sharp(path)
    .trim()
    .resize(175, 180, {
        fit: 'contain',
        background: {
            r: 225,
            g: 227,
            b: 237,
        }
    })
    .extend({
        left: 5,
        right: 5,
        top: 0,
        bottom: 0,
        background: {
            r: 225,
            g: 227,
            b: 237,
            alpha: 0
        }
    })
    .toFile(`trimmed_${path}`, (err) => {
            err && console.log('Error trimming file:' + err)
        })
}

Are you able to provide a sample image that helps explain the question?

I would like to be able to take all of the off-white (225,227,237) area that edges up against the model of these teeth and turn it white and transparent. I acquired this photo with the code above.

Here is my input file:

beforeLower-6caee480-ab4e-11e8-a183-6d4ab07faa80

Here is my output file:

trimmed_beforeLower-6caee480-ab4e-11e8-a183-6d4ab07faa80

I would like if all the outside area was transparent. It would be great if I could call something like

.replace({ input: {// color I want to replace here}, output: {// color I want it to be} })

enhancement

Most helpful comment

Any progress?

All 4 comments

Hello, although libvips provides some "flood fill" features, they are not yet exposed in sharp. I'll tag this as a future possible enhancement.

For now your best bet is to use raw pixel output then loop over and selectively replace values.

Hello, although libvips provides some "flood fill" features, they are not yet exposed in sharp. I'll tag this as a future possible enhancement.

For now your best bet is to use raw pixel output then loop over and selectively replace values.

lovell <- thank you so much for your quick response! I wasn't sure how to implement your suggestion, but it lead me to a new line of questioning and I ended up using the following package: https://github.com/turakvlad/replace-color

Perhaps it can be of use to you if building out this enhancement. I really appreciate all your awesome work and the use of your package!

I'm also looking forward to add the floodfill feature to this labrary, I was about to move from ImageMagick to Sharp, but without this feature I can't.
I would also help to have this feature!

Any progress?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Andresmag picture Andresmag  路  3Comments

natural-law picture natural-law  路  3Comments

AVVS picture AVVS  路  3Comments

paulieo10 picture paulieo10  路  3Comments

kachurovskiy picture kachurovskiy  路  3Comments