Skip to content
MagickCmd › ImageMagick rotate and flip commands

ImageMagick rotate and flip commands

Rotate and flip images with ImageMagick. Fix sideways phone photos with -auto-orient, rotate by any angle, and understand the difference between -flip and -flop.

Fix a photo that appears sideways

Phones store photos in the sensor's orientation plus an EXIF tag telling viewers how to rotate them. Some software honours that tag, some ignores it — which is why the same file looks correct in one app and rotated in another.

magick input.jpg -auto-orient output.jpg

-auto-orient bakes the rotation into the actual pixels and clears the tag, so every viewer agrees.

Do this before cropping. If you crop first, your coordinates are measured against the unrotated image and the crop lands somewhere unexpected.

Rotate by a right angle

magick input.jpg -rotate 90 output.jpg     # clockwise
magick input.jpg -rotate 270 output.jpg    # counter-clockwise
magick input.jpg -rotate 180 output.jpg

Right-angle rotations are lossless in terms of pixel data, though re-encoding a JPEG still costs a generation of quality. If that matters, use jpegtran -rotate 90 instead, which rotates without re-encoding.

Rotate by an arbitrary angle

magick input.jpg -background white -rotate 15 output.jpg

A non-right-angle rotation enlarges the canvas and fills the new corners with -background. Set it to none and output PNG if you want those corners transparent:

magick input.png -background none -rotate 15 output.png

-flip versus -flop

FlagEffect
-flipVertical mirror — top becomes bottom
-flopHorizontal mirror — left becomes right

Nearly everyone guesses these the wrong way round the first time. The one you usually want, for making a photo look like a mirror selfie, is -flop.

Transpose and transverse

magick input.jpg -transpose output.jpg    # flip + rotate 90
magick input.jpg -transverse output.jpg   # flop + rotate 90

Straighten a scanned document

ImageMagick can estimate and correct a small skew automatically:

magick input.jpg -background white -deskew 40% +repage output.jpg

The percentage is a threshold, not an angle. 40% is a reasonable starting point for scanned text on white paper. Add -fuzz 5% -trim afterwards to crop the resulting border.

Batch-fix orientation across a folder

mkdir -p out
for f in *.jpg; do magick "$f" -auto-orient "out/$f"; done

Related

Copied