when I map data with code, both png and jpeg are converted with a white background though I have a condition to only do that when output ext is jpeg or jpg
See original GitHub issueI’m trying to convert a transparent logo to jpg and png. As you can see in my code below, I check to see if the file output ext is jpeg or jpg and if it is, it change the background to white and flatten it.
For some strange reason, when I map data with function, both png and jpeg are converted with a white background though I have a condition to only do that when output ext is jpeg or jpg
let local_file = '/tmp/output/295aadfd-f837-4ffb-b3a0-4a407cca54e0.png'
let data = [[700,
null,
'width',
'/tmp/output/295aadfd-f837-4ffb-b3a0-4a407cca54e0_10',
'png'],
[700,
null,
'width',
'/tmp/output/295aadfd-f837-4ffb-b3a0-4a407cca54e0_10',
'jpg'],
[1000,
null,
'width',
'/tmp/output/295aadfd-f837-4ffb-b3a0-4a407cca54e0_10',
'png'],
[1000,
null,
'width',
'/tmp/output/295aadfd-f837-4ffb-b3a0-4a407cca54e0_10',
'jpg']]
Promise.all(data.map(convert_image_sharp(local_file))).then(() => {
console.log('image convert done');
});
function convert_image_sharp(image_path) {
let image = sharp(image_path);
return data => image
.metadata()
.then(function (metadata) {
let inputs = beatify_input(data);
if (inputs['crop']) {
image.extract(inputs['crop'][0], inputs['crop'][1], inputs['crop'][2], inputs['crop'][3])
}
image.resize(inputs['width'], inputs['height']);
if (['jpg', 'jpeg'].includes(inputs['ext'])){
console.log(inputs['ext']);
image.background('white');
image.flatten();
}
return image.toFile(inputs['write_path']);
})
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Which image format to you use in your design? JPG, PNG or ...
PNG include images that contain text, graphics with hard edges, and elements that require transparent backgrounds like logos.
Read more >Image file type and format guide - Web media technologies
In this guide, we'll cover the image file types generally supported by web browsers, and provide insights that will help you select the...
Read more >ImageMagick v6 Examples -- Common Image Formats
First I will output using the PNG format... convert -size 60x60 xc:none -fill white -stroke black \ -draw 'circle 30, ...
Read more >Image Processing With the Python Pillow Library - Real Python
In this step-by-step tutorial, you'll learn how to use the Python Pillow library to deal with images and perform image processing.
Read more >Image File Formats: When to Use Each File Type - 99Designs
Typically, images are processed (adjusted for color, white balance, exposure, etc.) and then converted and compressed into another format (e.g. JPEG or TIFF)....
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
Got it and thank you 😃
forgive me…I had to learn node js within 48 hours
Here’s a possible (untested) version of
convert_image_sharp
that should behave in a way that I have understood yourdata.map
logic to expect.This question is not really specific to sharp and is more of a general JavaScript coding question suited to a site such as StackOverflow.