Anything can go here, in any language... except my native language Sinhala. Be cool... anybody is warmly welcomed! :)

K770i, Photography and Sorting Problem

Hi fellas, after few days... I'm back again with some cool cool stuff. My internship is over... and now I look for new career opportunities. Anyhow, I was not so quick to apply for a new job like others did. After completing my internship, I wanted a 'free time' of just one or two weeks to have a tour around Sri Lanka.

So, I just had the tour. Amazingly, it was to Jaffna. I live in  Southern Province and going for a tour in Jaffna is just a daydream for a busy man. However, in any tour I have two main intentions, photography and flirting :P . (however I was not able to photograph her since her mom was there :-O )

I have been using a Sony Ericsson K770i phone for more than one year and that phone has made me a photography freak. But recently, it had encountered a problem with it's memory stick reader compartment. This really made me upset as my phone now can't read memory sticks. However, by deleting some wallpapers, ringtones, themes and midlets I managed to make some free space of 15 MB in the phone's internal memory. This is just enough for 15 photos at maximum possible quality. Sad! :(

Luckily I found another phone (a Chinese phone without a brand) which had a memory stick installed and bluetooth also. So my plan was to transfer photos into the Chinese phone over bluetooth after taking each 15 photographs, and delete originals to make free space on my phone for new photos... just like using a revolver ;) . It worked, but since bluetooth file transfer took longer than expected each time I missed few imoprtant shots as well. :(

Sony Cyber-shot cameras use a specific file naming convention. It's the DSC (Digital Still Camera) prefix before the five-digit file number (i.e. DSC00001.JPG, DSC00002.JPG, ...). Due to my 'revolver methodology' sometimes when I make free space my phone seemed to reset the file number. This resulted duplicate filenames, and upon transfer those duplicates were added a Dup(xx) prefix in their filenames. However, thanks to the Chinese phone, during the three days of tour I have been able to take nearly 300 photos, including few panoramas.

After coming home I wanted to arrange all the photos in the correct timeline. Since file naming has already been messed up it was no use arranging them by name. So the next chance is to arrange them by the modification time (unix mtime). Due to some unknown reason, there were some small misconjusnctions in the series when arranging by mtime. It was like Elephant Pass coming before Kilinochchi when heading to Jaffna. :P

So here comes the sorting problem... it's not the Sorting Problem that we learn at the computer science lecture, although we use the tools built upon those theories. I realized that playing with ctime, mtime and atime is just a waste of time... so I was looking for another solution. Yes!, there is. It's the Exif data stored with each file. I can extract the DateTimeOriginal tag from Exif and arrange the file accordingly. I am a Linux geek, so rather than doing a web search for an automated GUI tool  I wanted to do it by myself.

So my ultimate plan was to write a bash script that will check date-time from each file and rename them in the correct order. Also, I wanted them to follow the same DSC convention. There's a handy command line tool for reading image exif on Linux. It's exifprobe. Just an apt-get install is enough to get it installed on the computer. With exifprobe we have another handy tool called exifgrep. Now let us go ahead.

Here we extract the origianal date-time:
$ exifgrep -n DateTimeOriginal DSC00001.JPG

and the output will look like,
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:10 12:04:16'    # DSC00001.JPG:

What I want to do is to list each file against it's OriginalDateTime and then sort with Unix sort command. So it's just one more simple step, (I wanted the output in a text file too)

$ exifgrep -n DateTimeOriginal *.JPG | sort > sorted.txt

Here's a random portion of the output I got,

JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 12:18:54'    # Dup(01)DSC00046.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 12:20:39'    # DSC00048.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 12:23:13'    # Dup(01)DSC00052.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 13:31:30'    # DSC00001.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 13:31:49'    # DSC00002.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 13:32:38'    # DSC00003.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 13:34:07'    # Dup(01)DSC00004.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 13:48:40'    # DSC00007.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 14:01:13'    # Dup(02)DSC00008.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 14:24:49'    # Dup(01)DSC00014.JPG:
JPEG.APP1.Ifd0.Exif.DateTimeOriginal = '2010:09:11 14:25:43'    # Dup(02)DSC00015.JPG:

Now, it's the time to write the actual shell script. It's not a big deal actually, as you can see, so I don't need to explain.

mkdir sorted
lines=`cat sorted.txt | wc -l`
for (( line=1; line<=$lines; line++ ))
  src=`cat sorted.txt | head -$line | tail -1 | awk '{ print $6 }' | cut -d: -f1`
  dest="DSC"`printf "%05d\n" $line`".JPG"
  cp $src sorted/$dest -v

Finally, after running this script I managed to get all photographs according to the timeline. I'll be writing a note about my Jaffna tour as soon as possible, and will be releasing some photographs under the Creative Commons Attribution-Share Alike 3.0 Unported license.

Few of them (such as File:JaffnaPeninsula.JPG) are already available on Wikipedia! :-)

See also:
Thanks for reading... have a nice day! :) :) :)


Subscription Options

 Subscribe in a reader


Enter your email address:

to inscrease

and make me happy. :)

Other Blogs by the Author

The Author
ශාකුන්තල | Shaakunthala's items Go to ශාකුන්තල | Shaakunthala's photostream
free counters
Related Posts with Thumbnails