rotate images from the Linux command line

Two tools for rotating images from the command line are jpegtran and exiftran.

jpegtran

jpegtran transforms JPEG files. It can translate the coded representation from one variant of JPEG to another, for example from baseline JPEG to progressive JPEG or vice versa. It can also perform some rearrangements of the image data, for example turning an image from landscape to portrait format by rotation. The transpose transformation has no restrictions regarding image dimensions. jpegtran's default behaviour when transforming an odd-size image is designed to preserve exact reversibility and mathematical consistency of the transformation set.

The command to rotate input.jpg 90 degrees clockwise and save the result to output.jpg is:

    jpegtran -rotate 90 input.jpg output.jpg

All switch names may be abbreviated; for example, -optimize may be written -opt or -o. Upper and lower case are equivalent. British spellings are also accepted (e.g., -optimise), though for brevity these are not mentioned below.

TRANSFORM OPTIONS

  • -flip horizontal Mirror image horizontally (left-right).
  • -flip vertical Mirror image vertically (top-bottom).
  • -rotate 90 Rotate image 90 degrees clockwise.
  • -rotate 180 Rotate image 180 degrees.
  • -rotate 270 Rotate image 270 degrees clockwise (or 90 ccw).

The transpose transformation has no restrictions regarding image dimensions. The other transformations operate rather oddly if the image dimensions are not a multiple of the iMCU size (usually 8 or 16 pixels), because they can only transform complete blocks of DCT coefficient data in the desired way. The other transforms can be built up as sequences of transpose and flip operations; for consistency, their actions on edge pixels are defined to be the same as the end result of the corresponding transpose-and-flip sequence.

exiftran

exiftran is a command line utility to transform digital image jpeg images. It can do lossless rotations like jpegtran, but unlike jpegtran it cares about the EXIF data: It can rotate images automatically by checking the exif orientation tag, it updates the exif information if needed (image dimension, orientation), it also rotates the exif thumbnail. It can process multiple images at once.

TRANSFORM OPTIONS

  • -a automatic (using exif orientation tag)
  • -9 rotate by 90 degrees clockwise
  • -1 rotate by 180 degrees clockwise
  • -2 rotate by 270 degrees clockwise
  • -f flip vertical
  • -F flip horizontal
  • -t transpose
  • -T transverse
  • -nt don't rotate exif thumbnail.
  • -ni don't rotate jpeg image. (e.g., you might need this or or the -nt option to fixup things in case you rotated the image with some utility which ignores the exif thumbnail. Just generating a new thumbnail with -g is another way to fix it.
  • -no Don't update the orientation tag. By default exiftran sets the orientation to "1" (no transformation needed) to avoid other exif-aware applications try to rotate the already-rotated image again.

To autorotate all jpeg files in the current directory:

    exiftran -ai *.jpeg

References


View or Post Comments