lwip icon indicating copy to clipboard operation
lwip copied to clipboard

Auto-rotate image according to EXIF orientation

Open sebchevrel opened this issue 10 years ago • 9 comments

when loading JPEGs, the EXIF orientation flag is ignored, so the image may be incorrectly rotated (width and height are swapped)

sebchevrel avatar Jan 06 '15 20:01 sebchevrel

would like this as well..

Is there a work-around at the moment?

mikedavies-dev avatar Jan 23 '15 15:01 mikedavies-dev

EXIF is JPEG-specific. There is no equivalent for the other formats. This kind of metadata is not directly in the scope of image processing, which is why I haven't included any EXIF handling methods in this library. It is, however, very much related to image processing. I may consider implementing it in the future.

In the meantime I suggest using some external library to obtain EXIF information. There are a few of those implemented in pure JS.

EyalAr avatar Jan 25 '15 09:01 EyalAr

Please provide a link to at least one compatible.

ivan-kleshnin avatar Jan 26 '15 08:01 ivan-kleshnin

@ivan-kleshnin Google of node exif yields several results. I've used https://github.com/gomfunkel/node-exif in conjunction with this lib (and node Promise) to normalize image rotation based on exif data.

cookch10 avatar Mar 12 '15 02:03 cookch10

This feature would be greatly appreciated. It seems that the majority of my iPhone photographs start with the wrong orientation.

skeisman avatar May 22 '15 19:05 skeisman

The fact that an image appears incorrectly rotated is subjective. This is due to the medium used to view the image (os, app, browser, etc.) and whether that medium is accounting for the exif data in the image. See this article for more information.

cookch10 avatar May 22 '15 20:05 cookch10

For those looking for a solution, I used exif-parser, here's an example below using the batch function:

var fs = require('fs');
var exif = require('exif-parser');
var lwip = require('lwip');

// path is the path to your image
fs.readFile(path, function (err, data) {
  if (err) throw err;
  var exifData = false;
  // ext is the extension of the image
  if(ext == "jpg"){
    exifData = exif.create(data).parse();
  }
  lwip.open(data, ext, function(err, image){
    if(err) throw err;
    if(exifData){
      switch( exifData.tags.Orientation ) {
        case 2:
        image = image.batch().flip('x'); // top-right - flip horizontal
        break;
        case 3:
        image = image.batch().rotate(180); // bottom-right - rotate 180
        break;
        case 4:
        image = image.batch().flip('y'); // bottom-left - flip vertically
        break;
        case 5:
        image = image.batch().rotate(90).flip('x'); // left-top - rotate 90 and flip horizontal
        break;
        case 6:
        image = image.batch().rotate(90); // right-top - rotate 90
        break;
        case 7:
        image = image.batch().rotate(270).flip('x'); // right-bottom - rotate 270 and flip horizontal
        break;
        case 8:
        image = image.batch().rotate(270); // left-bottom - rotate 270
        break;
      }
    }else{
      image = image.batch();
    }
    // image can now be used as per normal with batch
    // eg. image.resize(200, 200)....
  });
});

carlosingles avatar Oct 28 '15 04:10 carlosingles

thanks carlosingles!

mnovinger avatar Dec 08 '15 18:12 mnovinger

Just run into this myself, this feature would indeed be nice to have. Without it, it's hard to handle images coming from an iphone camera. In the meanwhile it would be helpful to place a hint to use "exif-parser" to handle JPEG rotation in the README along with the rest. Wouldn't hurt to include a link to a gist of @carlosingles 's wonderful solution.

antoniobrandao avatar May 31 '16 00:05 antoniobrandao