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.

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 issue

I’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:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wobengcommented, Oct 2, 2018

Got it and thank you 😃

forgive me…I had to learn node js within 48 hours

0reactions
lovellcommented, Oct 2, 2018

Here’s a possible (untested) version of convert_image_sharp that should behave in a way that I have understood your data.map logic to expect.

function convert_image_sharp(image_path) {
  return () => {
    const image = sharp(image_path);
    const { crop, ext, width, height, write_path } = beatify_input(data);
    if (crop) {
      image.extract(...crop);
    }
    image.resize(width, height);
    if (['jpg', 'jpeg'].includes(ext)) {
      image.background('white').flatten();
    }
    return image.toFile(write_path);
  };
}

This question is not really specific to sharp and is more of a general JavaScript coding question suited to a site such as StackOverflow.

Read more comments on GitHub >

github_iconTop 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 >

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