Git Product home page Git Product logo

jpeg-autorotate's Introduction

Version Downloads Last commit Test Coverage Install Size

Icon

A node module to rotate JPEG images based on EXIF orientation.


What does it do

This module applies the right orientation to a JPEG image, based on its EXIF tag. More precisely, it:

  • Rotates the pixels
  • Rotates the thumbnail, if there is one
  • Writes 1 in the Orientation EXIF tag (this is the default orientation)
  • Updates the PixelXDimension and PixelYDimension EXIF values
  • Does not alter the other EXIF tags

It may be useful, if:

  • You need to compress your image with a tool that strips EXIF data without rotating the pixels (like the great ImageOptim)
  • You need to upload the image, but the destination application does not support EXIF orientation (like WordPress)
  • You just want to get rid of the orientation tag, while leaving the other tags intact

More information about EXIF:

Installation

This module needs Node >=14.

Install with npm:

$ npm install jpeg-autorotate --global
# --global isn't required if you plan to use the node module

Usage

Options

Option Default value Description
quality 100 Quality of the JPEG. Uncompressed by default, so the resulting image may be bigger than the original one.
jpegjsMaxResolutionInMP jpeg-js default maxResolutionInMP option in jpeg-js (doc)
jpegjsMaxMemoryUsageInMB jpeg-js default maxMemoryUsageInMB option in jpeg-js (doc)

CLI

Rotate a single image:

$ jpeg-autorotate /Users/johan/IMG_1234.jpg

Rotate a set of images:

$ jpeg-autorotate /Users/johan/images/IMG_*.jpg

Glob support:

$ jpeg-autorotate "/Users/johan/images/IMG_*.{jpg,jpeg,JPG,JPEG}"

Passing options:

$ jpeg-autorotate /Users/johan/IMG_1234.jpg --quality=85 --jpegjsMaxResolutionInMP=1234

Node module

The Node module will load the image, apply the rotation, and return the binary data as a Buffer, allowing you to:

  • Save it on disk
  • Load it in an image processing module (like jimp, lwip, gm...)
  • ...

Sample usage

const jo = require('jpeg-autorotate')
const options = {
  quality: 8,
  jpegjsMaxResolutionInMP: 1234,
}
const path = '/Users/johan/IMG_1234.jpg' // You can use a Buffer too

//
// With a callback:
//
jo.rotate(path, options, (error, buffer, orientation, dimensions, quality) => {
  if (error) {
    console.log('An error occurred when rotating the file: ' + error.message)
    return
  }
  console.log(`Orientation was ${orientation}`)
  console.log(`Dimensions after rotation: ${dimensions.width}x${dimensions.height}`)
  console.log(`Quality: ${quality}`)
  // ...Do whatever you need with the resulting buffer...
})

//
// With a Promise:
//
jo.rotate(path, options)
  .then(({buffer, orientation, dimensions, quality}) => {
    console.log(`Orientation was ${orientation}`)
    console.log(`Dimensions after rotation: ${dimensions.width}x${dimensions.height}`)
    console.log(`Quality: ${quality}`)
    // ...Do whatever you need with the resulting buffer...
  })
  .catch((error) => {
    console.log('An error occurred when rotating the file: ' + error.message)
  })

Error handling

The error object returned by the module contains a readable message, but also a code for better error handling. Available codes are the following:

const jo = require('jpeg-autorotate')

jo.errors.read_file // File could not be opened
jo.errors.read_exif // EXIF data could not be read
jo.errors.no_orientation // No orientation tag was found
jo.errors.unknown_orientation // The orientation tag is unknown
jo.errors.correct_orientation // The image orientation is already correct
jo.errors.rotate_file // An error occurred when rotating the image

Example:

const jo = require('jpeg-autorotate')
jo.rotate('/image.jpg')
  .catch((error) => {
    if (error.code === jo.errors.correct_orientation) {
      console.log('The orientation of this image is already correct!')
    }
  })

Troubleshooting

Thumbnail too large

The piexifjs module has a 64kb limit when reading thumbnails. If you get the Given thumbnail is too large error, you can try to remove the thumbnail from the image before rotating it:

import piexif from 'piexifjs'

function deleteThumbnailFromExif(imageBuffer) {
  const imageString = imageBuffer.toString('binary')
  const exifObj = piexif.load(imageString)
  delete exifObj['thumbnail']
  delete exifObj['1st']
  const exifBytes = piexif.dump(exifObj)
  return Buffer.from(piexif.insert(exifBytes, imageString), 'binary')
}

Changelog

This project uses semver.

Version Date Notes
9.0.0 2023-02-11 Drop Node 12 support
8.0.1 2022-01-10 Remove colors package from dependencies
8.0.0 2021-11-12 Node 16 support
Drop support for Node 10
7.1.1 2020-10-11 Introduce code coverage
Fix an error if options are not passed
7.1.0 2020-10-10 Introduce jpegjsMaxResolutionInMP & jpegjsMaxMemoryUsageInMB options (#26)
7.0.0 2020-09-19 Don't publish test and linting files on NPM
6.0.0 2020-05-30 Dependencies update
Drop support for Node < 10
From jpeg-js update: images larger than 100 megapixels or requiring more than 512MB of memory to decode will throw
5.0.3 2019-12-24 Fix multiple file support in CLI
Dependencies update
5.0.2 2019-09-28 Dependencies update
5.0.1 2019-06-08 Fix CLI support
5.0.0 2019-03-03 Drop --jobs CLI option
Drop support for Node 6 & 7
Introduce new quality property in the jo.rotate callback
Public API now supports both callbacks and Promises
Update documentation accordingly
Update dependencies
4.0.1 2018-11-29 Fix rotations 5 and 7 (issue #11)
4.0.0 2018-07-15 Drop support for Node 4 & 5
Unpublish lockfile
Use prettier for code formatting
Update documentation
Update dependencies
3.1.0 2017-12-03 Output dimensions after rotation
3.0.1 2017-07-30 Node 8 support
Update dependencies
3.0.0 2017-02-11 CLI supports glob
No more node 0.12 support
Drop semicolons
Add eslint rules
2.0.0 2016-06-03 Supports buffers in entry
Returns a buffer even if there was an error
Improves tests
1.1.0 2016-04-23 Adds test suite, removes lwip dependency
1.0.3 2016-03-29 Displays help when no path given in CLI
1.0.2 2016-03-21 Adds missing options in CLI help
1.0.1 2016-03-21 Fixes NPM publishing fail ^_^
1.0.0 2016-03-21 Initial version

License

This project is released under the MIT License.

Contributing

Bug reports and feature requests are welcome! More details in the contribution guidelines.

Credits

jpeg-autorotate's People

Contributors

bikalay avatar icebob avatar johansatge avatar lukaselmer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jpeg-autorotate's Issues

Wildcard support not working correctly in cli

Wildcard and glob support in the CLI appears to be only reading the first file encountered. If I have a directory with the following files:

  • test1.jpg
  • test2.jpg
  • test3.jpg

and I run jpeg-autorotate test*.jpg, it will correctly process the first item and then no more.

How to pass Buffer as argument instead of path? How to solve following error?

Basically, I have image info in base64 encoded. so, I'm converting the base64 to buffer by using the following code

const thumbnailBuffer = await Buffer.from(thumbnailBase64, "base64");
The Buffer im passing looks like this to the rotate() function.
buffer <Buffer 75 ab 5a 8a 66 a0 7b fa 67 81 b6 ac 7b ae 22 54 13 91 c3 42 86 82 80 00 00 03 52 52 11 14 80 00 00 3b c0 00 00 3b c2 00 80 00 00 25 44 85 3b 80 00 00 ... 8626 more bytes>
Below is the error when Im executing.

An error occurred when rotating the file: Could not read EXIF data (Error: 'load' gots invalid file data.)
(node:21457) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'width' of undefined
    at PDFDocument.image (/home/user/***/***/***/node_modules/pdfkit/js/mixins/images.js:27:17)
    at WriteStream.<anonymous> (/home/user/***/***/***/***/***/file.js:192:11)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:21457) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:21457) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Done in 2.17s.

Please LMK if Im doing any wrong.

Not compatible with node 10

When trying to install the module with node 10.4.1 it produces the following error:

error [email protected]: The engine "node" is incompatible with this module. Expected version "9.3.0".
error Found incompatible module
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
PS C:\Projects\project> node -v
v10.4.1

Glob as CLI path

Maybe I'm just missing something to get it right, but it seems like the tool does not support glob paths am I right there? I tried the following:

jpeg-autorotate input/**/*.{jpg,jpeg,JPG,JPEG}

but keep getting:

> jpeg-autorotate input/**/*.{jpg,jpeg,JPG,JPEG}

input/**/*.{jpg,jpeg,JPG,JPEG}: Could not read file (ENOENT: no such file or directory,
open 'C:\Test\input\**\*.{jpg,jpeg,JPG,JPEG}')

What am I doing wrong? Hints are very welcome ;-)

Keep Exif data

Thanks for the awesome plugin!

Any thoughts on adding an option to reapply exif data after rotate?

I can see one would have to update the orientation tag.

Just saw that you have an option to regenerate the thumbnail. Awesome.

cli has no affect?

hello my friend, i was happy to find this simple tool

however when i ran this on my images, even though it reported that it correctly detected which images have the botched orientation, and says that they were processed -- the images appear totally unchanged, the cli tool appears to have had no affect

here is my bash session

Chase@Chase-PC MINGW64 ~/Desktop/Lonnie Photoshoot 2
$ jpeg-autorotate ./*.jpg
./20190602_125845.jpg: Orientation already correct
./20190602_125957.jpg: Orientation already correct
./20190602_130009.jpg: Orientation already correct
./20190602_130014.jpg: Orientation already correct
./20190602_130026.jpg: Orientation already correct
./20190602_130101.jpg: Orientation already correct
./20190602_130104.jpg: Orientation already correct
./20190602_130230.jpg: Orientation already correct
./20190602_130231.jpg: Orientation already correct
./20190602_130233.jpg: Orientation already correct
./20190602_130235.jpg: Orientation already correct
./20190602_130239.jpg: Orientation already correct
./20190602_130242.jpg: Orientation already correct
./20190602_130243.jpg: Orientation already correct
./20190602_130246.jpg: Orientation already correct
./20190602_130247.jpg: Orientation already correct
./20190602_130250.jpg: Orientation already correct
./20190602_130252.jpg: Orientation already correct
./20190602_130253.jpg: Orientation already correct
./20190602_130254.jpg: Orientation already correct
./20190602_130255.jpg: Processed (Orientation was 6) (Quality 100%)
./20190602_130256.jpg: Processed (Orientation was 6) (Quality 100%)

the last two images, after the processing, appear totally unchanged

just now i have used git to confirm that not a single byte has changed

how can i get jpeg-autorotate to save the upright processed image?

Visual testing

As seen in #11, the existing tests are not enough.

Let's try to introduce visual testing, to make sure the resulting image has been rotated/flipped correctly.

Possible workflow:

  • After rotating the test images, write them on disk
  • Capture the original image with an EXIF-friendly tool (by taking a screenshot with Chrome + puppeteer, for instance?)
  • Run a visual comparison between the two images (with pixelmatch for instance)

EACCES: permission denied

jpeg-autorotate upload/20170220204242-image.jpg

upload/20170220204242-image.jpg: EACCES: permission denied, open 'upload/20170220204242-image.jpg'

I don't want to use sudo. Any Idea?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.