Open In App
Related Articles

How to convert PNG to JPG using Node.js ?

Improve Article
Save Article
Like Article

The following approach covers how to convert PNG to JPG in Node.js using Jimp module. Jimp is an image processing library that we can use to do a lot of operations on images. Jimp stands for JavaScript Image Manipulation Program.

Approach: We will use the following steps:

  1. Import Jimp module in our application.
  2. Read PNG image in Jimp module.
  3. Convert PNG to JPG using Jimp functionalities.
  4. Return the final JPG Image.

Setting up environment and Execution:

Step 1: Initialize node.js project with the following command.

npm init

Step 2: Install the required module using the following command.

npm install jimp

Step 3: Get one sample PNG file, for this example, we have taken the below image and placed it in the static folder.

Project Structure: It should look like the following:

Step 4: Create an index.js file with the following code.


// Import jimp module
const Jimp = require("jimp");
// Read the PNG file and convert it to editable format"./static/GFG_IMG.png", function (err, image) {
    if (err) {
        // Return if any error
    // Convert image to JPG and store it to 
    // './output/' folder with 'out.jpg' name

Step 5: Run node.js project using the following command.

node index.js

Output: See the JPG output in the output folder.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Apr, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials