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 functionality fails with error: Unexpected error while trimming. Try to lower the tolerance #1597

Closed
Granga opened this issue Mar 1, 2019 · 11 comments

Comments

@Granga
Copy link

Granga commented Mar 1, 2019

In the latest version 0.21.3, trimming a png image fails.
In a previous version 0.20.8, the code completes successfully.

Here's my function in typescript:

async trim(dataUrl: string) {
    dataUrl = dataUrl.replace(/^data:image\/png;base64,/, "");
    dataUrl += dataUrl.replace('+', ' ');

    let buffer = new Buffer(dataUrl, "base64");

    return sharp(buffer)
        .png()
        .trim(1) //also tried default parameter
        .toBuffer();
}

This is the output in console:
Error: Unexpected error while trimming. Try to lower the tolerance.

@lovell
Copy link
Owner

lovell commented Mar 1, 2019

Hello, the following entry for v0.21.0 in the changelog is relevant here:

Switch from custom trim operation to vips_find_trim. #914
https://sharp.pixelplumbing.com/en/stable/changelog/#v0210-4th-october-2018

Please can you provide a sample image that behaves in this manner.

@lovell lovell added the question label Mar 1, 2019
@Granga
Copy link
Author

Granga commented Mar 2, 2019

Thank you for your response @lovell.
I generate the image's dataUrl with fabricjs inside puppeteer, so it's basically canvas.toDataUrl().

Both 'jpeg' and 'png' produce this error. Here's the png that won't trim:
5c79acd0d45450aa28dd8c8d

@Granga
Copy link
Author

Granga commented Mar 2, 2019

My node version is v10.15.1 on Windows10.

I did another test with the uploaded png:
Opened it with mspaint and just saved it in a new file.
It lost the transparent background, but sharp v0.21.3 trimmed it successfully.

@papandreou
Copy link
Contributor

Then we just need a way to automate mspaint :)

@lovell
Copy link
Owner

lovell commented Mar 5, 2019

@Granga Thank you for the example PNG. I think this has something to do with the input being 2 channel greyscale+alpha but I'll need to take a closer look.

@lovell lovell added triage and removed question labels Mar 5, 2019
@lovell
Copy link
Owner

lovell commented Mar 17, 2019

All of the pixels in this image are black and the text is contained only in the transparency layer.

By default sharp/libvips flattens to black hence the entire image is background.

We need to retrieve the background colour from the bKGD chunk in the PNG, if present, and flatten to that instead.

@lovell lovell added enhancement and removed triage labels Mar 17, 2019
@kapouer
Copy link
Contributor

kapouer commented Jun 28, 2019

Hi,
using sharp 0.22.1 i get the same error when trying to trim() an empty image. Since resize doesn't crash on those, it would be nice if trim did not crash too.

@stefanak-michal
Copy link

stefanak-michal commented Jul 3, 2019

Why you don't add vips_find_trim as sharp function to just get the information
https://jcupitt.github.io/libvips/API/current/libvips-arithmetic.html#vips-find-trim

I need to execute this on fully transparent picture to get right coordinates

vips find_trim --threshold 0 --background 0 image.png

With current implementation it's impossible.

@lovell
Copy link
Owner

lovell commented Aug 22, 2019

An alternative approach would be to modify libvips to optionally include the alpha channel in the calculation.

@lovell
Copy link
Owner

lovell commented Nov 14, 2019

Commit c41b873 adds this test case and the code change to handle it, which is to fallback to using the alpha channel to determine "boringness" if the initial search of the non-alpha channels fails.

@lovell lovell added this to the v0.23.3 milestone Nov 14, 2019
@lovell
Copy link
Owner

lovell commented Nov 17, 2019

v0.23.3 now available, thank you for the original report.

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

5 participants