question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some PNGs result in a black alpha channel, most don't.

See original GitHub issue

I have a REST call on my server that takes in a buffer of an image sent from the client along with information for cropping.

The code works 99% of the time but every so often I encounter a PNG image that does not preserve the alpha channel and it ends up black.

This is the (simplified) code:

(github does not want to format this code nicely for some reason… so here is a screenshot as well) image

if (fileType === 'jpeg' || fileType === 'jpg') { console.log('Is jpg making backgroung alpha 1'); s = s.background({r: 255, g: 255, b: 255, alpha: 1}); } else { s = s.background({r: 255, g: 255, b: 255, alpha: 0}); s = s.png({progressive: true, force: true}); } s = s.extend(padding); if (fileType === 'jpeg' || fileType === 'jpg') { s = s.flatten(); } s .toBuffer((err, buffer) => { s = sharp(buffer); } }

I do some cropping and calculating variable padding prior to that.

PNG it works with:

cofb_website_logo

PNG it does not work with: swinging with the stars - 2019 logo - vegas theme

This is the result of the second one after running it through the code. campaignname1231logo-h1brvoyf7-h1lscvuftq-swinging with the stars - 2019 logo - vegas theme png

I cannot figure out what is going on, maybe some more knowledgeable help on the subject or the PNG fits an edge case?

Note: the call to .png() was added today to see if forcing it to png would help, it didn’t, and has been working for most PNGs without it up till now.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yharaskrikcommented, Oct 11, 2018

@mjgallag I just updated to 0.21.0 and unfortunately it seems that the “swinging with the starts” image still comes out black. Even after moving the deprecated background call to the .extends call I had below.

0reactions
lovellcommented, Dec 4, 2018

Closing due to inactivity but please feel free to re-open with more details, including a standalone code sample, if the problem persists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do PNG files sometimes have 'black' backgrounds even ...
PNG files may have an alpha channel (levels of transparency for each pixel), but not all software that can save images as PNG,...
Read more >
Png not opening with alpha channel
Solved: photoshop incorrectly opens pngs with transparency instead of using a proper alpha channel. this is a long standing bug any word as ......
Read more >
The background of my PNGs aren't becoming transparent :(
Following on from Howard-Vigorita's comment, the alpha channel (full opacity/transparency) will usually display on the png image as a grey ...
Read more >
Problem about Background transparent .png format OpenCV ...
Since your input image already contains an alpha channel, the solution is simple -- just reuse the alpha channel. There's a slight problem...
Read more >
Why is my PNG being imported with a black background in ...
The PNG is absolutely transparent. There were PNGs that i had imported before without any problem. I used to get the transparency. But...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found