lwip
lwip copied to clipboard
Auto-rotate image according to EXIF orientation
when loading JPEGs, the EXIF orientation flag is ignored, so the image may be incorrectly rotated (width and height are swapped)
would like this as well..
Is there a work-around at the moment?
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.
Please provide a link to at least one compatible.
@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.
This feature would be greatly appreciated. It seems that the majority of my iPhone photographs start with the wrong orientation.
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.
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)....
});
});
thanks carlosingles!
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.