- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to resize an image in Node Jimp?
NodeJS – Resize() is an inbuilt function that is used to resize the images to the desired size. We can use resize to set the height and width using a 2-pass bilinear algorithm. It can resize an image into any size as declared by the user. We can take input from the user or resize it into fixed Width*Height size.
Syntax
resize(w, h, mode, cb)
Definition of resize() paramters
w – This parameter is used to declare the width of the image. This is a required parameter.
h – This parameter is used to declare the height of the resized image. This parameter is also required.
mode – This is an optional parameter that is used to store the scaling method.
cb – This is also an optional parameter that can be invoked after the compilation is complete.
Input Image
Using Node JIMP – RESIZE()
Before proceeding to use resize() functions, please check that the following statements are already executed for setting up the environment.
npm init -y // Initialising the Node environment
npm install jimp --save // Installing the jimp dependency
Create a resize.js file and copy-paste the following code snippet in it.
Use node resize.js to run the code.
Note – The method name should match with the JS file name. Only then it will be able to call the desired method.
Example
const Jimp = require('jimp'); async function resize() { // Function name is same as of file name // Reading Image const image = await Jimp.read ('/home/jimp/tutorials_point_img.jpg'); image.resize(200,200, function(err){ if (err) throw err; }) .write('/home/jimp/resize.jpeg'); } resize(); // Calling the function here using async console.log("Image is processed successfully");
Output
Using Node JIMP – RESIZE() with 'cb' optional parameters
Example
const Jimp = require('jimp') ; async function resize() { // Reading Image const image = await Jimp.read ('/home/jimp/tutorials_point_img.jpg'); // Used RESIZE_BEZIER as cb for finer images image.resize(1024,768,Jimp.RESIZE_BEZIER, function(err){ if (err) throw err; }) .write('/home/jimp/resize.jpeg'); } resize(); console.log("Image is processed successfully");
Output
- Related Articles
- How to flip an image in Node Jimp?
- How to rotate an image in Node Jimp?
- How to change an image to greyscale in Node Jimp?
- How to apply Posterize to an image in Node Jimp?
- How to overlay an image over another in Node Jimp?
- How to blur an image using Node Jimp blur() function?
- How to crop an image using crop() function in Node Jimp?
- How to invert the colors of an image in Node Jimp?
- How to change the opacity of an image in Node Jimp?
- How to apply a Sepia tone to an image in Node Jimp?
- How to adjust the contrast of an image using contrast() function in Node Jimp?
- How to resize an Image C#?
- How to resize an image using Tkinter?
- How to resize an image in Android using Picasso?
- How to resize an image in OpenCV using Python?
