Question / Feature Request: Autofix/Correct orientation

Started by boustanihani, June 21, 2025, 06:43:50 AM

Previous topic - Next topic

boustanihani

Question / Feature Request: Autofix/Correct orientation

Can we pass an argument to let Exiftool autocorrect/fix the Orientation a an image?
If not, please consider this as a feature request :)

This is what photoshop does when we simply open an image and overwrite it:

- If the JPG contains an EXIF orientation tag (common in photos from smartphones/cameras), Photoshop reads this tag and automatically rotates the image for display/editing.
- The image appears correctly oriented in Photoshop, even if the original pixel data is stored sideways or upside-down.
- When you save (using File > Save or Ctrl+S/Cmd+S), Photoshop saves the current visual state of the image (i.e., the rotated orientation).
- The EXIF orientation tag is removed (set to 1, meaning "no rotation needed") because the pixels are now permanently stored in the correct orientation.
- Result: The file is rewritten with fixed orientation, and the tag is stripped. Other software (browsers, viewers) will display it correctly without relying on EXIF.


StarGeek

Exiftool only edits metadata. It cannot affect image data, which is what you are asking.

Additionally, what you are doing is a destructive recompress which deletes image data (see Reddit ELI5, Know Your Meme, and various More JPEG sites). This results in a "blockier" image with compression artifacts.

Example:


Instead of loading and re-saving, look for a program that does "JPEG Transforms". JPEGs can be losslessly rotated as long as the width/height is divisible by 8 or 16 (varies by JPEG). If you're on Windows, then Irfanview with the "JPG_TRANSFORM" plugin can do this. XNView can also do JPEG transforms and is multi-platform.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

boustanihani

I need to automate this on a server. Do you possibly know a good node.js library for autorotation?

StarGeek

I do not.

But all JPEG transform programs are based upon jpegtran, and you might see if that's available on your server. jhead is a program that will use jpegtran to correctly rotate the images for you and is also available on some servers.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

boustanihani

jhead looks interesting, so if I do "jhead -autorot image.jpg" will the image get autorotated and will all metadata (EXIF, IPTC, XMP, etc.) remain preserved?

StarGeek

According to the jhead source code, when it calls jpegtran, it uses the -copy all option, which will copy all the APP## jpeg blocks, which includes EXIF, IPTC, and XMP metadata, as well as other types.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

boustanihani

I ended up using: https://sharp.pixelplumbing.com/api-operation/#autoorient

await sharp(input).autoOrient().toFile(output);

And finally copying the metadata back to the autorotated file..