Sobes.tech
Junior — Middle

How can you specify the image format when working with code?

sobes.tech AI

Answer from AI

In Node.js, the image format can be specified when working with images using various libraries, such as sharp or jimp. These libraries allow converting images to the desired format, such as JPEG, PNG, WebP, and others.

Example using sharp to convert an image to PNG format:

const sharp = require('sharp');

sharp('input.jpg')
  .png() // set format to PNG
  .toFile('output.png')
  .then(() => {
    console.log('Image saved in PNG format');
  })
  .catch(err => {
    console.error('Error processing image:', err);
  });

Thus, the format is set by calling the corresponding method (.png(), .jpeg(), .webp(), etc.) before saving or outputting the image.

How can you specify the image format when working… - sobes.tech