Replace transparency with a colour
See original GitHub issueHi,
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:
- Created 6 years ago
- Reactions:1
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can achieve this with compositing…
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 😦