Exif

From miki
Jump to navigation Jump to search

EXIF (Exchangeable image file format) is a standard format for metadata in images, sounds...

Interesting links

Tools

  • extract
    Behave like file command, but for metadata (support many formats)
  • Exiv2
    Very powerful, support rename and update of file timestamps
  • jhead

Commands

extract -V file.jpg        # Short verbose info on file.jpg
exiv2 -p s file.jpg        # List EXIF data
exiv2 mv -t *.jpg          # Rename & change JPEG file timestamp
exiv2 mv -T *.jpg          # *Only* change JPEG file timestamp

Scripts

From [1]

First recover the files, and sort them a bit:

photorec /d rescuedir disk_image
find rescuedir -type f | cut -d. -f2 | sort | uniq -c | sort -n    # View file types and quantities

A script to rename jpg files according to their timestamps, and detect duplicates (I updated it to include cases where newname is set to -). Note also that tool exiv2 handle rename and setting timestamp so this script is maybe obsolete.

#! /bin/bash
#
# Update: handle case where "$newname" = "-"

trash=trash$$
unknown=unknown$$
mkdir $trash
mkdir $unknown
for file in f*.jpg; do
  newname=`exiv2 -p s $file | grep -a timestamp | awk '{ print $4 "-" $5 }' | sed 's/:/-/g'`
  if [ $? -eq 0 -a "x$newname" != "x" -a "x$newname" != "x-" ]; then
    if [ ! -f ${newname}.jpg ]; then
      mv $file ${newname}.jpg
    else
      cmp $file ${newname}.jpg
      if [ $? -eq 0 ]; then
        echo "WARN: $file is a duplicate of ${newname}.jpg: moved to $trash"
        mv $file $trash
      else
        echo "ERR: ${newname}.jpg already exists but differs. $file unchanged"
      fi
    fi
  else
      echo "ERR: unable to get info from ${file}: moved to $unknown"
      mv $file $unknown
  fi
done

Sort files in different directories:

for file in [12][0-9][0-9][0-9]-*.jpg; do
 dirname=`echo $file | cut -d- -f1` # ou ${file:0:4} en bash
 if [ ! -d $dirname ]; then
   mkdir $dirname
 fi
 mv $file $dirname
done

Some custom scripts

Sort files by size:

#! /bin/bash
#
# Example: sortsize.sh rescue jpg
#
#          Look for 'jpg' file in directory 'rescue', and sort them (create a
#          new directory 'jpg' in current directory.

if [ -z "$1" ]; then
    echo Usage: $0 directory extension
    exit 1
fi

ext=$2
MICRO=${ext}/_1_micro
TINY=${ext}/_2_tiny
SMALL=${ext}/_3_small
MEDIUM=${ext}/_4_medium
BIG=${ext}/_5_big

mkdir -p $MICRO $TINY $SMALL $MEDIUM $BIG
while read SIZE FILE; do
    if (( $SIZE < 1000 )); then
        mv $FILE $MICRO/
    elif (( $SIZE < 10000 )); then
        mv $FILE $TINY/
    elif (( $SIZE < 100000 )); then
        mv $FILE $SMALL/
    elif (( $SIZE < 1000000 )); then
        mv $FILE $MEDIUM/
    else
        mv $FILE $BIG/
    fi
done < <(find "$1" -type f -iname "*.$ext" -print0 |xargs -0 stat -c "%s %n")

Sort JPG files by camera type:

exiv2 -p s *.jpg 2>/dev/null | perl -lne 'print for /Camera make.*: (.*)$/' | sort -u | tr '\n' '\0' | xargs -0 mkdir
for file in *.jpg; do
  CAMERA=$(exiv2 -p s $file 2>/dev/null | perl -lne 'print for /Camera make.*: (.*)$/');
  if [ -n "$CAMERA" -a -d "$CAMERA" ]; then
    mv $file "$CAMERA"/;
  fi;
done