10

I have a mess in my photos folder; I want to sort them according to date in EXIF information and rename according to the date (like 001.jpg, 002.jpg and so on).

How can I do this in Linux? I have used ImageMagick for some basic bulk processing tasks before (converting and resizing, etc), is it possible to use it for this task?

7 Answers 7

24

You can use exiftool. For some reason the online manual does not contain the "RENAMING EXAMPLES" section which gave me the essential hint.

For JPG only files the following command invocation should do the job:

exiftool -r '-FileName<CreateDate' -d '%Y-%m-%d/%H_%M_%S%%-c.%%le' <yourFolder>

Explanation:

  • -r is for recursion
  • '-FileName<CreateDate' tells exiftool to rename the file accordingly to its EXIF tag CreateDate (you can try others like ModifyDate though)
  • -d '%Y-%m-%d/%H_%M_%S%%-c.%%le' tells how to build the filename string from the date source "CreateDate" (the "%-c" will append a counter in case of file collisions, the "%le" stands for "lower cased file extension")
  • Note: I used "-FileName<..." here for renaming the files and moving it to another folder within one step. The manual points out that you have to use the "-Directory<..." syntax for folder operations. It worked for me this way though.

You should spend some time reading the manual of this powerful tool. Maybe there is an even shorter way :D

1
5

Try this free product: AmoK Exif Sorter.

2

The most simple way is to just import files into jbrout ... then (optionally) all files can have names changed to something time oriented. Or you may find, that you do not need changing names at all (and jbrout is sufficient).

1

I did a small and not very optimized python script that at I think does what you intend:

1 import pyexiv2, sys, os, time, datetime, random
2 dirname = sys.argv[1]
3 flist=os.listdir(dirname)
4 dic = dict()
5 for fname in flist:
6     metadata = pyexiv2.ImageMetadata(sys.argv[1]+'/'+fname)
7     metadata.read()
8     tag = metadata['Exif.Image.DateTime']
9     timestamp = int(time.mktime(tag.value.timetuple()))
10     #print fname, ' ', tag.value, ' ', timestamp
11     try:
12         dic[timestamp] = fname
13     except:
14         print fname, ' not processed.'
15 
16 keys = dic.keys()
17 keys.sort()
18 i = 0 
19 for k in keys:
20     os.rename(sys.argv[1]+'/'+dic[k], sys.argv[1]+'/'+str(i)+'.jpg')
21     i+=1
1
1

To make it work in Debian Lenny try this:

import pyexiv2, sys, os, time, datetime, random
dirname = sys.argv[1]
flist=os.listdir(dirname)
dic = dict()
for fname in flist:
    image = pyexiv2.Image(sys.argv[1]+'/'+fname) 
    #metadata = pyexiv2.ImageMetadata(sys.argv[1]+'/'+fname)
    #metadata.read()
    image.readMetadata() 
    tag = image['Exif.Image.DateTime']
    timestamp = int(time.mktime(tag.timetuple()))
    #print fname, ' ', tag.value, ' ', timestamp
    try:
    dic[timestamp] = fname
    except:
    print fname, ' not processed.'

keys = dic.keys()
keys.sort()
i = 0 
for k in keys:
    os.rename(sys.argv[1]+'/'+dic[k], sys.argv[1]+'/'+str(i)+'.jpg')
    i+=1
1

In php I did something like this:

<?php

$path = "./path/to/images";
$files = scandir($path);

foreach ( $files as $file )
{
    if ( $file[0] === "." )
    {
        continue;
    }
    $ext = end(explode(".", $file));

    $suffix = "image";

    $exif = exif_read_data($path."/".$file, 'EXIF');
    $datetime = str_replace("/","-", $exif['DateTimeDigitized']);

    rename($path."/".$file, $path."/".$datetime.$suffix.".".$ext);
}
0

While we are at it, here is one which is more "gonzo programming" style, in bash, using exiftool, it can be let loose on several directories and/or individual files in one go. It skips files that have unrecognized mime types.

It renames photos using the DateTimeOriginal EXIF tag (so the result is not 001.jpg but 2015-12-22_14:43:15.jpg.

If there is a naming conflict, index numbers are applied: 2015-12-22_14:50:57.1.jpg.

May be useful: relabel_photo_with_datetimeoriginal.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .