Node bindings for the Epeg library
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Forked from (with node v6 support)

My fixes:

  • Issue with random memory for the "cropped" and "scaled" flags (code otherwise randomly fails)
  • Improved error messages and documentation.
    I highly recommend using my fork (this repository) over phorque's and L8D's versions.


  • Crop or scale down images with the insanely fast (and commonly used) C library libjpeg
  • While libjpeg isn't as fast as libjpeg-turbo, it's waaaay faster than anything written in JavaScript


  • NodeJS
  • libjpeg

To install libjpeg:

  • Mac
    bash brew install libjpeg

  • Ubuntu
    bash sudo apt-get -y install libjpeg

  • Windows
    I have absolutely no idea

Install epeg (once libjpeg installed)

Run this command in your project directory. The --save will add this repository to your package.json

npm install git:// --save


Load image binary

// Load from path - image on disk
var image = new epeg.Image({path: "./test.jpg"}));


// Load from binary data - after something like fs.readFile
var image = new epeg.Image({data: imgBinaryBuffer}));


var epeg = require("epeg");

var pixelsFromLeftToCrop = 100;
var pixelsFromTopToCrop = 50;
var newWidth = 200;
var newHeight = 300;

image = new epeg.Image({path: "./test.jpg"}));
image.crop(pixelsFromLeftToCrop, pixelsFromTopToCrop, newWidth, newHeight).saveTo("./output.jpg");


Scale an image down

var epeg = require("epeg");

var newWidthInPixels = 150;
var newHeightInPixels = 50;
var saveQualityPercent = 90;

image = new epeg.Image({path: "./test.jpg"}));
image.downsize(newWidthInPixels, newHeightInPixels, saveQualityPercent).saveTo("./ugly.jpg");

Quality parameter

Note that crop and downsize can take an optional extra 'quality' parameter between 1 and 100 (default is 85)

To get raw image buffer

Instead of saving with .saveTo(filename), you can call .process() after downsize/crop

var fs = require("fs");
var epeg = require("epeg");

fs.readFile("./test.jpg", function(err, data) {
  // load the image with the binary buffer
  var image = new epeg.Image({data: data});
  var rawImageBuffer = image.downsize(100, 100).process();
  // do whatever with the rawImageBuffer, maybe do a phash or something for image similarity?
  // write changed image to disk
  fs.writeFileSync("./output.jpg", buffer);

Credits - Original repository - added crucial node v6 support - fixed memory flag runtime error and added docs