• Willkommen im Linux Club - dem deutschsprachigen Supportforum für GNU/Linux. Registriere dich kostenlos, um alle Inhalte zu sehen und Fragen zu stellen.

[Script] Umbenennen von Fotos nach Exif-Zeitstempel

taki

Advanced Hacker
Mit dem folgenden Script kann man alle Fotos eines Verzeichnisses umbenennen. Es ermittelt aus den EXIF-Daten (mit identify aus dem ImageMagic-Paket) den Zeitpunkt, zu dem jedes Foto geschossen wurde und bennent alle danach um.

Wenn mehrere Fotos den gleichen Zeitstempel haben, wird ein Index hochgezählt, damit keine Fotos durch Überschreiben verloren gehen. Wenn eine Datei schon den Dateinamen hat, den sie nach dem Zeitstempel bekäme, wird sie vom Umbenennen ausgenommen.

Das Ermitteln des Zeitstempel dauert einige Zeit. Man muss bei größeren Verzeichnissen Geduld mitbringen (habe das ganze auf ein Verzeichniss mit 803 Fotos angewendet :)).

Zwei temporäre Dateien werden angelegt: Eine Liste der gefundenen Bilder mit dem Zeitstempel aus den Exif-Daten (tmp.lst) und ein Script, mit dem letztenendes das Umbennenen ausgeführt wird (tmp.sh). Die Dateien können hinterher gelöscht werden. Das Löschen habe ich aber nicht eingebaut. So kann man sich das Ergebnis noch einmal anschauen :mrgreen: .

Sicher kann man die Aufgabe eleganter lösen, aber sie funktioniert zuverlässig und für meine Ansprüche schnell genug.

http://www.andreas-silberstorff.de/misc/getExifTimestamp.sh
 

TeXpert

Guru
nette Sache :) aber ich möchte nicht versäumen zu sagen: There is more than one way....


-> jhead kann das schon

man jhead schrieb:
-n[format_string]
This option causes files to be renamed using the date information from the Exif header "DateTimeOriginal" field. If the file is not an Exif file, or
the DateTimeOriginal does not contain a valid value, the file date is used. Renaming is by default restricted to files whose names consist largely of
digits. This effectively restricts renaming to files that have not already been manually renamed, as the default sequential names from digital cameras
consist largely of digits. Use the -nf option to force renaming of all files.

If the format_string is omitted, the file will be renamed to MMDD-HHMMSS. Note that this scheme doesn't include the year (I never have photos from dif-
ferent years together anyway).

If a format_string is provided, it will be passed to the strftime function as the format string. In addition, if the format string contains '%f', this
will substitute the original name of the file (minus extension). '%i' will substitute a sequence number. Leading zeros can be specified like with
printf - i.e. '%04i' pads the number to 4 digits using leading zeros.

If the target name already exists, the name will be appended with "a", "b", "c", etc, unless the name ends with a letter, in which case it will be
appended with "0", "1", "2", etc.

This feature is especially useful if more than one digital camera was used to take pictures of an event. By renaming them to a scheme according to
date, they will automatically appear in order of taking in most directory listings and image browsers. Alternatively, if your image browser supports
listing by file time, you can use the -ft option to set the file time to the time the photo was taken.

Some of the more useful arguments for strftime are:

%H Hour in 24-hour format (00 - 23)
%j Day of year as decimal number (001 - 366)
%m Month as decimal number (01 - 12)
%M Minute as decimal number (00 - 59)
%S Second as decimal number (00 - 59)
%w Weekday as decimal number (0 - 6; Sunday is 0)
%y Year without century, as decimal number (00 - 99)
%Y Year with century, as decimal number

Example:

jhead -n%Y%m%d-%H%M%S *.jpg This will rename files matched by *.jpg in the format YYYYMMDD-HHMMSS

For a full listing of strftime arguments, look up the strftime in them man pages. Note that some arguments to the strftime function (not listed here)
produce strings with characters such as '/' and ':' that may not be valid as part of a filename on various systems.
 
OP
taki

taki

Advanced Hacker
So etwas nennt man wohl Konvergenz: Wenn es für eine Aufgabe eine halbwegs ideale Umsetzung gibt, kommt man automatisch zum gleichen Ergebnis.

jhead kannte ich noch nicht, hätte mir die Arbeit vielleicht erspart, andererseits hätte es mir auch den Spaß am Entwickeln verdorben, wenn ich das vorher gekannt hätte :? Ausserdem finde ich ganz nett, dass ich meinem Script keine Optionen mit geben brauche. Auch habe ich gerne das Jahr mit im Namen, ob es nun Sinn macht oder nicht (was ist z.B. mit den Silvester-Fotos? Da hätten wir schon mal Bilder aus zwei verschiedenen Jahren im Kasten).

P.S.: "apt-get install jhead" läuft gerade ;-)
 
OP
taki

taki

Advanced Hacker
Das Ermitteln dauerte wohl irgendwas zwischen 20 Minuten und einer halben Stunde (hab nicht die Zeit gestoppt). Das Umbenennen selbst geht ganz fix.
 

Calvin

Hacker
@taki

kleiner Verbesserungsvorschlag. Den Aufruf von tmp.sh solltest du in
Code:
./tmp.sh
ändern. Bei mir wurde es sonst nicht ausgeführt.
 
Oben