Sunday 21 November 2010

Organize your photos by date in nautilus

I already know how to use the exif information in pyhton to group photos in folders by date the picture was taken( see here ). The problem is that you must run the script in a terminal and the script must by in the python path. So I changed it a bit. Now the script can be used in the Nautilus. To group photos in any directory I can select Scripts-> Group By Date.

Changes from the previous script are basically three.
On the first line put the path to the python, for example #!/usr/bin/python
Check if there are any selected files using NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
And finally show a message when the script is finished.
Here's the script:
#!/usr/bin/python

import os
from PIL import Image
import shutil

x = None
try:
    x = os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']
    x = x.split('\n')
except:
    pass

if len(x)>1:
    files = [f.split(os.path.sep)[-1:][0] for f in x if f.endswith('.JPG') or f.endswith('.jpg')]
else:
    files = [f for f in os.listdir('.') if f.endswith('.JPG') or f.endswith('.jpg')]

cdate_tag = 0x9003
for f in files:
    if os.path.isfile(f):
        img = Image.open(f)
        date = img._getexif()[0x9003].split(' ')[0].replace(':','_')
        if not os.path.isdir(date):
            os.mkdir(date)
        shutil.move(f,os.path.join(date,f))
        img.fp.close()
import gtk
dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, "Done!")
dialog.run()
dialog.destroy()

No comments:

Post a Comment