Rotating an image from the terminal is useful when an asset has the right content but the wrong orientation for a page, thumbnail, or handoff. ImageMagick can write a rotated copy while leaving the original file in place for comparison.
The -rotate operator takes degrees. A quarter-turn such as 90 changes a 1200x800 landscape file into an 800x1200 portrait file, and the verification step should compare both files before replacement.
Use ImageMagick 7 magick syntax when it is available. Right-angle rotations are the safest batch pattern; arbitrary angles can expose corner triangles filled from the current background color, and camera files with EXIF orientation may need -auto-orient before a manual rotation.
Related: Crop an image with ImageMagick
Related: Resize an image with ImageMagick
Steps to rotate an image with ImageMagick:
- Open a terminal in the directory containing the source image.
- Check the source image dimensions before rotating it.
$ magick identify -format '%f %m %wx%h\n' source.jpg source.jpg JPEG 1200x800
The sample source is a landscape JPEG. Use the actual filename and expected orientation for the image being prepared.
- Rotate the image and write the result to a new file.
$ magick source.jpg -rotate 90 rotated.jpg
Use 90 for one quarter-turn, -90 for the opposite quarter-turn, or 180 for an upside-down image. Keep the output filename different from the input until the result has been checked.
- Confirm that the rotated file reports the expected dimensions.
$ magick identify -format '%f %m %wx%h\n' source.jpg rotated.jpg source.jpg JPEG 1200x800 rotated.jpg JPEG 800x1200
A 90 or -90 rotation swaps width and height. A 180 rotation keeps the same dimensions.
- Open the rotated image before replacing the original file or updating an asset reference.
If the image appears correct in a viewer but exports sideways, check whether the file has EXIF orientation metadata. Apply -auto-orient before a manual rotation only when that metadata needs to be baked into the pixels.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.