sharp icon indicating copy to clipboard operation
sharp copied to clipboard

trim() fails on blank black image

Open juliangruber opened this issue 3 years ago • 2 comments

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • [x] Running npm install sharp completes without error.
  • [x] Running node -e "require('sharp')" completes without error.

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

  • [x] I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

  System:
    OS: macOS 12.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 1.96 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.0/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm

What are the steps to reproduce?

Call .trim() on an image that is pure black. This error is thrown:

Error: Unexpected error while trimming. Try to lower the tolerance

What is the expected behaviour?

out.png is created. Since it's all black I'm not sure if trim should noop here or create an empty image. We call trim() on images uploaded by users, where we don't know beforehand if they can be trimmed or not. If .trim() behaves correctly here, do you know of a way to find out if an image can't be trimmed beforehand?

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

sharp(`${__dirname}/in.png`)
  .trim(1)
  .toFile(`${__dirname}/out.jpg`)

Please provide sample image(s) that help explain this problem

in

juliangruber avatar May 11 '22 11:05 juliangruber

it's all black I'm not sure if trim should noop here or create an empty image.

Hi, yes, the key question is what happens when using trim() with an image where all pixels have the same value, including the alpha channel. Is this an error, the current behaviour, or a no-op as proposed? I do like your suggestion as it should improve the user experience.

This is somewhat related to https://github.com/lovell/sharp/issues/2166, which I think that should be implemented first, before altering the default nothing-to-trim behaviour.

Let's mark this as an enhancement. Happy to accept a PR for #2166 and/or this issue, if you're able.

lovell avatar May 11 '22 13:05 lovell

Commit https://github.com/lovell/sharp/commit/cbf741cac7cc78ef20d5962131d5c6b83630046d switches the behaviour from error to no-op. This will be in v0.31.0.

lovell avatar Jul 08 '22 20:07 lovell

v0.31.0 now available with this improvement, thank you for the suggestion.

lovell avatar Sep 05 '22 09:09 lovell