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.

selecting color of background when converting from png to jpg

See original GitHub issue

Converting a png with a transparent background to jpg will change that color to black by default. Is there anyway to change this color? I can’t see to find any API that can do this. It seems like .background would be a candidate but the documentation isn’t very clear on what it does.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
targoscommented, Jul 4, 2019

Hi, to do this you would need to convert the transparent pixels to white. Here is a way this can be done:

// Load the image.
const image = await Image.load('image.png');
// Get the alpha channel.
const alpha = image.getChannel(3);
// Create a binary mask on the transparent pixels (255 = opaque).
const mask = alpha.mask({ threshold: 254, invert: true });
// Paint the pixels from the mask in white.
image.paintMasks(mask, { color: 'white' });
// Save the image as jpg.
await image.save('image.jpg');

.background is another method that tries to remove the “noise” in the background of an image to enhance it for use in other algorithms.

0reactions
toyssamuraicommented, Feb 24, 2020

There’s a problem with this solution – the mask is binary and the anti-aliasing edges would be lost. Does anyone know of a way that can keep the anti-aliasing?

Read more comments on GitHub >

github_iconTop Results From Across the Web

PNG to JPEG: how is background color determined? - Legacy ...
Hi, I'm converting some PNGs to JPEG using "-alpha background". With some images the background color is white and with others it is...
Read more >
How to add a white background to PNG images with a ...
To change the color, double click on the color icon in the fill layer and select another color. 4. Save as JPG and...
Read more >
Make JPG and PNG Logos One Color Vector and ... - YouTube
In this video I show you how to use Image Trace to convert PNG and JPG logos to vector and remove the white...
Read more >
How to quickly change the color of a PNG file in Adobe Illustrator
Did you know Illustrator has a way to quickly change the color of a PNG file (similar to Photoshop)? Learn how in this...
Read more >
How to Convert JPG To PNG Image With A Transparent ...
Note: Select "Yes" in the " Choose a color to make transparent?" section to ensure that your converted JPG Images background will be...
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