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

Black outlines in image are removed when using trim() #2265

Closed
curiousdustin opened this issue Jun 18, 2020 · 5 comments
Closed

Black outlines in image are removed when using trim() #2265

curiousdustin opened this issue Jun 18, 2020 · 5 comments
Labels

Comments

@curiousdustin
Copy link

version: sharp@0.25.4

What are the steps to reproduce?

Attempt to trim the attached example file.

sharp(inputFilePath)
    .trim()
    .toFile(outputFilePath)

Input

animal-0002

Output

animal-0002

What is the expected behaviour?

Black outlines would be preserved.

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

  System:
    OS: macOS 10.15.4
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 47.95 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 13.12.0 - ~/.nvm/versions/node/v13.12.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - ~/.nvm/versions/node/v13.12.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman```
@curiousdustin
Copy link
Author

This seems related to #1246, #1797, and #1597, which are all closed but my issue persists.

@lovell
Copy link
Owner

lovell commented Jun 19, 2020

The black outline in this image is part of the alpha channel - please see #2166 for a future possible enhancement to address this.

@lovell lovell added question and removed triage labels Jun 19, 2020
@curiousdustin
Copy link
Author

Is there another manipulation I can do prior to trimming to work around this issue? To somehow move the outline out of the alpha channel?

@curiousdustin
Copy link
Author

I have found that if I flatten and removeAlpha it trims as expected. Except of course then I have the background color from the flatten.

I was then able to using the offsets mentioned in the docs, to crop (extract) the original image with alpha.

The info response Object will contain trimOffsetLeft and trimOffsetTop properties.
const flattened = await sharp(inputFilePath)
    .flatten({ background: { r: 255, g: 0, b: 255 } })
    .removeAlpha()
    .toBuffer();

sharp(flattened)
  .trim()
  .toBuffer((err, data, info) => {
    const crop = {
      left: -info.trimOffsetLeft,
      top: -info.trimOffsetTop,
      width: info.width,
      height: info.height,
    };

    return sharp(inputFilePath)
      .extract(crop)
      .toFile(outputFilePath)
  });

This is probably a sluggish work around, but it appears to work well enough for my use case.

@curiousdustin
Copy link
Author

Closing in favor of #2166

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