Rename/resize pictures with the command line

Here are two command lines I use all the time to manage pictures on Linux: one to rename files with their shooting date (using exiv2) and the other to resize them to a given with (using mogrify).

Renaming

Renaming pictures according to their shooting date is a straightforward way to merge photo sets coming from multiple cameras. On Linux, the exiv2 tool does the trick:

exiv2 -r'%F_%H-%M-%S_:basename:' rename $(ls \*.JPG)

The date format is the same as in the standard date command (where %F is a shortcut for %Y-%m-%d). If you omit the :basename: part of the pattern, which is automatically replaced with the file's original name, you may run into multiple photos getting the same file name: in this case, exiv2 will ask whether to overwrite, rename or skip them. As far as I know, you can't get more time precision than seconds from EXIF metadata.

Resizing

I usually resize images to an 800-pixel width before sending, which can be done seamlessly using the mogrify script from ImageMagick:

mogrify -resize 800 -auto-orient $(ls \*.JPG)

By default, this tool preserves the image aspect-ratio, so you don't need to specify the full output resolution and the width (800) is enough.

Cameras usually store picture orientation in the image's metadata, while pixel data corresponds to the camera's sensor coordinates. That is, when you take a vertical photo, your camera just stores what it sees, and then adds a flag in the metadata saying "this should be rotated by +/-90 degrees". Image viewers interpret this flag correctly, but web browsers and e-mail clients do not (e.g. when you insert the image directly in an e-mail); this is why I threw the auto-orient flag here, which automatically performs the orientation on the pixel data.

Pages of this website are under the CC-BY 4.0 license.