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.

Replace transparency with a colour

See original GitHub issue

Hi,

I’m currently converting PNG (&/BMP/GIF) to JPG. When I convert images, any transparency (alpha channels) turn BLACK. I would like to change this to WHITE.

Is there a way to do this out of the box?

1. Apply image on top of a white background

Jimp.read(file).then(function(image) {
    var width = image.bitmap.width;
    var height = image.bitmap.height;
    var white = 0xFFFFFFFF;
    // create a white background...
    new Jimp(width, height, white, function(err, backgroundImage) {
        if (err) {
            return handleError(err);
        }
        // apply image on top of white background...
        backgroundImage.blit(image, 0, 0)
            .getBuffer(Jimp.MIME_JPEG, function(err2, buffer) {
                // do stuff with image
            });
    });
});

2. Manually alter each pixel

Jimp.read(file).then(function(image) {
    image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx) {
        var alphaModifier = 1 - (this.bitmap.data[idx + 3] / 255);
        var red = (255 - this.bitmap.data[idx + 0]) * alphaModifier;
        var green = (255 - this.bitmap.data[idx + 1]) * alphaModifier;
        var blue = (255 - this.bitmap.data[idx + 2]) * alphaModifier;
        this.bitmap.data[idx + 0] += red;
        this.bitmap.data[idx + 1] += green;
        this.bitmap.data[idx + 2] += blue;
        this.bitmap.data[idx + 3] = 255; // set no transparency
    });
    // do stuff with image...
});

Sample Source Images SourceImages.zip

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

5reactions
itslennycommented, Oct 25, 2018

You can achieve this with compositing…

const width = 300;
const height = 300;
const image = // a jimp image with transparent background
const outputPath = 'where/to/save/it';

new Jimp(width, height, '#FFFFFF', (err, bgImage) => {
    bgImage.composite(image, 0, 0)
        .write(outputFilePath,
            (err) => {
                console.log('file created!!');
            });
});
3reactions
Ojas-Gulaticommented, Aug 8, 2017

You can set a “default color” in Jimp. Let me look into that…

EDIT: try this: image.background(0xFFFFFFFF); and then convert the image.

I don’t have my normal computer, so can’t test 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

White(or any specified color) to Transparent - TechLagoon
On this page, you can convert the white part of the uploaded image to transparent. This is useful for removing white backgrounds from...
Read more >
How to Replace Colors With Transparent in GIMP
Click "Color" from the menu bar and select "Color to Alpha." The Color to Alpha dialog window opens and shows a small preview...
Read more >
2 Ways To Make Any Color Transparent In Photoshop (Fast!)
To make a color transparent in Photoshop, go to Select > Color Range. Click on the color you want to remove, then hold...
Read more >
Fill Transparent PNG Regions - Online PNG Maker
World's simplest online utility that replaces transparent color in PNGs. Free, quick, and powerful. Import a PNG – replace transparent PNG sections.
Read more >
Replace white background with transparent online - IMGonline
By default the white or single-color background of the image is replaced with a transparent one. The background color of the source image...
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