Resizing and Correctly Orienting Images with Mogrify

Lately, I’ve been publishing woodworking tutorials over on my own blog, and I’ve found myself needing to resize and correctly orient images taken with my iPhone prior to posting.

If you want to do anything with images on Linux, the best place to start is with ImageMagick, an all-in-one utility that does just about everything under the sun with images, and has fantastic documentation to boot.

After a bit of research, I came up with this command:

mogrify -auto-orient -resize 584×438 -strip -quality 85% *.jpg

Here’s a rundown of exactly what this does:

  • mogrify modifies the images in the source directory. This command will overwrite your image files, so use it on a copy of them if you care to keep the original sized images
  • auto-orient fixes image orientation. When you take a picture with your iPhone, it writes the image directly to storage in the orientation that the image sensor was in at the time the image was taken, and then writes that orientation information to the image’s metadata. This makes taking pictures faster, because the phone doesn’t have to rotate the image prior to writing the file, but can cause images to show up in all sorts of creative orientations, depending on the software tool chain that you use to get the image onto your website
  • resize resizes the image to the specified size (expressed in pixels). If the image can’t be resized to the exact dimensions specified, it will be taken to the nearest possible dimensions, while maintaining aspect ratio, so the end image won’t be all stretched out
  • strip removes all metadata from the final images. This is handy for privacy, since you probably don’t want the GPS coordinates of your home being published online, but also removes the pesky orientation metadata, which is good, because the auto-orient command already acted on it, and you wouldn’t want an image to be double-rotated at display time
  • quality specifies the quality of the final jpg. I’ve found that 85% works well, but if you’re quality-conscious, you could set this too 100% and it won’t take much extra time to convert the images
  • *.jpg executes the command against all .jpg images in the current directory

So there you have it! A simple one-line command that rotates and resizes images prior to posting them. Oh, and if you’re the type that tends to forget complicated command-line syntax, don’t forget about the history command:

jon@IDEAPAD-UBUNTU:~$ history | grep mogrify
1691 mogrify -auto-orient -resize 584×438 -strip -quality 85% *.jpg
1696 history | grep mogrify

It’s an easy way to find that pesky command that you know you’ve used in the recent past, without resorting to looking up all of the command line switches again.

Cheers, and happy image uploading!

This post was adapted from the original post on my blog at jonathanfritz.ca




Be the first to comment

Leave a Reply

Your email address will not be published.


*