Jimp (JavaScript Image Manipulation Program)

1.6.1 · active · verified Sun Apr 19

Jimp is a comprehensive image processing library for Node.js, written entirely in JavaScript with zero native dependencies. It enables various image manipulation tasks such as resizing, cropping, color adjustments, and format conversions without external binaries. The current stable version is 1.6.1, reflecting an active development and release cadence with multiple patch and minor versions in recent months. Key differentiators include its pure JavaScript implementation, which ensures high portability across diverse environments, and a modular plugin architecture for customized builds. Jimp supports a wide range of formats, including JPEG, PNG, BMP, TIFF, and GIF, making it a versatile choice for server-side image processing workflows.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read an image, perform common manipulations like resizing, converting to grayscale, adding text, and saving the output to a new file. It includes basic error handling and uses modern ESM syntax.

import { Jimp } from 'jimp';
import path from 'path';
import fs from 'fs/promises';

async function processImage() {
  const inputPath = path.resolve('./input.png');
  const outputPath = path.resolve('./output-processed.jpg');
  const fontPath = path.resolve(__dirname, '../../node_modules/@jimp/plugin-print/fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt'); // Example font path

  try {
    // Ensure input.png exists for demonstration
    // For a real app, you'd fetch or use an existing file
    // await fs.writeFile(inputPath, Buffer.from('...', 'base64')); // Example: write a placeholder image

    const image = await Jimp.read(inputPath); // Read the image
    
    // Resize, grayscale, and add text
    const font = await Jimp.loadFont(fontPath);
    
    image
      .resize(300, Jimp.AUTO) // resize to 300px width, auto height
      .quality(80) // set JPEG quality
      .greyscale() // set greyscale
      .print(font, 10, 10, 'Hello Jimp!') // add text
      .write(outputPath); // save

    console.log(`Image processed and saved to ${outputPath}`);
  } catch (error) {
    console.error('Error processing image:', error);
  }
}

// Create a dummy input.png for the quickstart if it doesn't exist
async function createDummyImage() {
  const dummyImagePath = path.resolve('./input.png');
  if (!await fs.stat(dummyImagePath).catch(() => false)) {
    const dummyImage = await new Jimp(100, 100, 0xFF0000FF); // 100x100 red image
    await dummyImage.write(dummyImagePath);
    console.log(`Created dummy image at ${dummyImagePath} for quickstart.`);
  }
}

createDummyImage().then(() => processImage());

view raw JSON →