Convert Between TIFF & BMP Files Using Node.js & Jimp

Introduction
Do you need to convert BMP
images to a TIFF
format? Or TIFF
files to a BMP
format? This article will walk you through how to do that quickly and easily using Node.js and the Jimp NPM package.
Let's get started!
Table of Contents
Install Jimp NPM Package
First, you need to install the NPM package. You can install it with either the NPM or yarn command below:
NPM:
npm install jimp --save
Yarn:
yarn add jimp
Now we're ready to start writing some code and converting images!
Convert TIFF to BMP
Let's convert a .tiff
file to a .bmp
format first. Make sure you have a .tiff
file in the root of your project directory that you want to convert to a .bmp
.
Here's the full code:
const Jimp = require("jimp")
Jimp.read("file.tiff", function (err, file) {
if (err) {
console.log(err)
} else {
file.write("new-image.bmp")
}
})
Let's break down each part of the code:
- First, we import the
jimp
NPM package and hold it in theJimp
variable. - Then, we use the
Jimp.read()
function to process the image into something we can work with. We pass the path of thefile.tiff
file to the function. And the function returns a promise with anerror
(if it exists) and a file to work with. - We do some error handling inside the callback function.
- Then, we write the new
new-image.bmp
file to the current directory with thefile.write()
function.
And you should see the new-image.bmp
file written to your directory.
Make sense? Cool! Let's move onto the next example in the next section.
Convert BMP to TIFF
Now let's convert a .bmp
file to a .tiff
format. Make sure you have a .bmp
file in the root of your project directory that you want to convert to a .tiff
.
Here's the full code:
const Jimp = require("jimp")
Jimp.read("image.bmp", function (err, file) {
if (err) {
console.log(err)
} else {
file.write("new-file.tiff")
}
})
Here's the break down of each part of the code:
- First, we import the
jimp
NPM package and hold it in theJimp
variable. - Then, we use the
Jimp.read()
function to process the image into something we can work with. We pass the path of theimage.bmp
file to the function. And the function returns a promise with anerror
(if it exists) and a file to work with. - We do some error handling inside the callback function.
- Then, we write the new
new-file.tiff
file to the current directory with theimage.write()
function.
And you should see the new-file.tiff
file is written to your directory.
Conclusion
Now you know how to convert back and forth between TIFF and BMP files using Node.js and Jimp!
Thanks for reading and happy coding!