sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Replace Color with transparency

Open yasso1am opened this issue 5 years ago • 4 comments

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} })

yasso1am avatar Apr 05 '19 17:04 yasso1am

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 avatar Apr 05 '19 18:04 lovell

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!

yasso1am avatar Apr 08 '19 17:04 yasso1am

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!

daton89 avatar Feb 02 '20 12:02 daton89

Any progress?

hengkx avatar Jun 09 '20 06:06 hengkx

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

thanks for providing an available idea here's a possible solution for replacing the white color with transparency:

const HEX = '0123456789ABCDEF';
const toHex = (value: number) => {
  let rtn = '';
  while (value !== 0)
    rtn = HEX[value % 16] + rtn, value = Math.floor(value / 16);
  return rtn;
}
const { data, info } = await origImg
  .raw()
  .toBuffer({ resolveWithObject: true });
for (let i = 0; i < data.length; i += 4)
  if (`#${toHex(data[i])}${toHex(data[i + 1])}${toHex(data[i + 2])}` === '#FFFFFF')
    data[i + 3] = 0;
const alphaImg = sharp(data, {
  raw: {
    width: info.width,
    height: info.height,
    channels: 4
  }
}).toFormat('png').toBuffer();

ENDsoft233 avatar Feb 28 '23 14:02 ENDsoft233

v0.32.1 now available with unflatten() operation - see https://sharp.pixelplumbing.com/api-operation#unflatten

PRs with further enhancements to this experimental feature are welcome.

lovell avatar Apr 27 '23 09:04 lovell