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

How to remove transparent pixels from all edges? #1246

Closed
obazna opened this issue Jun 1, 2018 · 2 comments
Closed

How to remove transparent pixels from all edges? #1246

obazna opened this issue Jun 1, 2018 · 2 comments
Labels

Comments

@obazna
Copy link

obazna commented Jun 1, 2018

Hi,

I have PNG files that contain huge margins around the content, empty spaces that no one wants to see.
Is there any way to detect and trim transparent pixels from edges without using
raw() data? Unfortunately, I didn't find it. Thanks for the help in advance.

@lovell
Copy link
Owner

lovell commented Jun 1, 2018

Hello, did you see the trim operation?

@lovell lovell added the question label Jun 1, 2018
@obazna
Copy link
Author

obazna commented Jun 1, 2018

Thanks for your response, @lovell
The problem was in my code. I have already fixed it. trim works perfectly fine.

I had the following code:

// convert .SVG to .PNG
await sharp(input)            
            .resize(width, height)
            .png()
            .trim()
            .toFile(output);

Input .SVG file

original

Expected result

expected

Actual result

actual
The bottom and top was cutted more than I neded. Bad. But I didn't give up :)

I played with code and found a solution. Now it works as expected.

let buffer = await sharp(input)            
            .resize(width, height)            
            .png()            
            .toBuffer();

        await sharp(buffer)
            .trim()
            .toFile(output);

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