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() fails on blank black image #3223

Closed
3 tasks done
juliangruber opened this issue May 11, 2022 · 3 comments
Closed
3 tasks done

trim() fails on blank black image #3223

juliangruber opened this issue May 11, 2022 · 3 comments

Comments

@juliangruber
Copy link

juliangruber commented May 11, 2022

Possible bug

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

  • Running npm install sharp completes without error.
  • 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?

  • 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

@lovell
Copy link
Owner

lovell commented May 11, 2022

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 #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
Copy link
Owner

lovell commented Jul 8, 2022

Commit cbf741c switches the behaviour from error to no-op. This will be in v0.31.0.

@lovell lovell added this to the v0.31.0 milestone Jul 8, 2022
@lovell
Copy link
Owner

lovell commented Sep 5, 2022

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

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

No branches or pull requests

2 participants