Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trim does not work as expected when using with joinChannel #3054

Closed
deepakg202 opened this issue Jan 22, 2022 · 4 comments
Closed

trim does not work as expected when using with joinChannel #3054

deepakg202 opened this issue Jan 22, 2022 · 4 comments
Labels

Comments

@deepakg202
Copy link

deepakg202 commented Jan 22, 2022

Are you using the latest version? Is the version currently in use as reported by npm ls sharp the same as the latest version as reported by npm view sharp dist-tags.latest?

Yes

What are the steps to reproduce?
I am trying to apply a b/w mask to an image and trim the transparent area. These are the input image and the mask.
Image
Mask
This is the code I am using:

const sharp = require('sharp');
const fs = require('fs');

applyMaskAndTrim = async (image, mask) => {
    await sharp(image).joinChannel(mask).trim(10).toFile('output.png');
}

(async () => {
    var imageFile = fs.readFileSync("image.jpg");
    var maskFile = fs.readFileSync("mask.png");

    await applyMaskAndTrim(imageFile, maskFile);
    console.log("Done");
})();

What is the expected behaviour?
The expected output should be Expected Image
But I get this Output Image
However I get the correct output when using joinChannel and trim separately on separate files.

Are you able to provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem?
See code sample above

Are you able to provide a sample image that helps explain the problem?
See above images

What is the output of running npx envinfo --binaries --system?

  System:
    OS: Windows 10 10.0.22000
    CPU: (8) x64 Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
    Memory: 2.43 GB / 7.83 GB
  Binaries:
    Node: 14.17.5 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.14 - C:\Program Files\nodejs\npm.CMD
@lovell
Copy link
Owner

lovell commented Jan 22, 2022

Hi, yes, you'll need to split these operations into two pipelines, as the trim will always occur first. You might also run into #2166.

@lovell lovell added question and removed triage labels Jan 22, 2022
@deepakg202
Copy link
Author

@lovell Thank you for the quick response.
Is there any other alternative to make the process faster as it adds additional time to encode the image twice.

@lovell
Copy link
Owner

lovell commented Feb 3, 2022

As an intermediate image format, you could use either raw pixel data or png with a compressionLevel of 0.

@lovell
Copy link
Owner

lovell commented Mar 14, 2022

I hope this information helped. Please feel free to re-open with more details if further assistance is required.

@lovell lovell closed this as completed Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants