← Back to team overview

ubuntu-photographers team mailing list archive

Re: script to sort out a bunch of photographs!

 

Hi,

I made a script for doing exactly this. It is a work in progress I always
wanted to finish. It used jhead for read the exif information and rotate the
images according to the exif information. I had plans on using exiv2 for
doing the same process raw images.

It also has some comments in spainsh... sorry. Hope it help at least as a
template

Fede

On Tue, Apr 6, 2010 at 12:45 PM, Graham Binns <graham@xxxxxxxxxxxxxxx>wrote:

> On 6 April 2010 11:31, louis taylor <louis@xxxxxxxxxxxxx> wrote:
> > Hello there everyone on the mailing list!
> >
> > I am writing here to ask the guidance of the ubuntu photographers about a
> > small problem I have.
> > I have about 3000 photographs (taken by about 6 different cameras) in a
> > filesystem with not too much order (photographs placed in a jumble of
> > directorys).
> > I would like to put these in a neat filesystem with all of the
> photographs
> > taken in 2009 in the folder '2009', with the photographs in this
> subdivided
> > into folders orded by the month, with these divided into the weeks.
> >
> > I could write a script to do this for me (by reading the metadata) in
> > python, and I was wondering if:
> > a) someone has made something like this before.
>
> I'm sure someone has - I've started many times but never finished
> because it's not something that I really needed (I already organised
> stuff by date, so most of my scripts worked by munging directory
> names).
>
> > b) this would be useful for any ubuntu photographer.
>
> Almost certainly. I know I'd use it on my older, non-date-sorted image
> archives.
>
> > c) there is something that I haven't thought of which makes this whole
> thing
> > quite difficult.
>
> I don't think so. What you need is a Python library that can read EXIF
> tags from images. That plus datetime.strptime() and you're away. I
> know there are Python EXIF libraries out there (ISTR that the PIL
> supports reading EXIF, but don't quote me); it's just a case of
> finding one that suits your cause best.
>
> But on the whole, go for it! I'd love to have something like this
> available.
>
> --
> Graham Binns | PGP Key: EC66FA7D
>
> _______________________________________________
> Mailing list: https://launchpad.net/~ubuntu-photographers
> Post to     : ubuntu-photographers@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~ubuntu-photographers
> More help   : https://help.launchpad.net/ListHelp
>
#!/usr/bin/env python
import os
import sys

import glob
import uuid
import popen2
import shutil

class PhotoManager:
    dest = '/home/feclare/fotos'
    dest = '/home/feclare/fotos_test'
    jheadComm = 'jhead -autorot ' 
    dest_undated = dest + '/sinfecha'

    def __init__(self):
        try:
            os.makedirs(self.dest)
        except OSError:
            pass

    def cleanup_photos(self):
        try:
            os.makedirs(self.dest_undated)
        except:
            pass
        for f in glob.glob(self.dest + '/*.jpg'):
            shutil.move(f, self.dest_undated)
    
    def proccess_photo(self, f):
        print 'Procesamos foto: ' + f
        (path, name) = os.path.split(f)
        nameFound = False
        while not nameFound:
            fdest = '%s/%s.jpg' % (self.dest, uuid.uuid4())
            try:
                os.stat(fdest)
            except :
                break

        shutil.copy(f, fdest)
        os.system(r'jhead -autorot ' + fdest)

        fds = popen2.popen2('jhead ' + fdest)

        dateFound = False
        while (True):
            l = fds[0].readline()
            if len(l) == 0:
                break
            if l.startswith('Date/Time'):
                t = l.split(':')
                y = t[1].strip()
                m = t[2]
                (d, h) = t[3].split(' ')
                min = t[4]
                # Copiar el fichero
                dateFound = True
                break

        if dateFound:
            newName = '%s/%s/%s/%s/%s_%s_%s-%s_%s.jpg' % (self.dest, y,m,d,y,m,d,h,min)
            nameFound = False
            i = 1
            while not nameFound:
                try:
                    os.stat(newName)
                except :
                    break
                newName = '%s/%s/%s/%s/%s_%s_%s-%s_%s_%d.jpg' % (self.dest, y,m,d,y,m,d,h,min, i)
                i += 1
            print newName, os.path.split(newName)[0]
            try:
                os.makedirs(os.path.split(newName)[0])
            except:
                pass
            shutil.move(fdest, newName)

if __name__ == '__main__':
    print 'TODO: Leemos valores por defecto de fichero de conf'
    print 'TODO: gestiomos los parametros de entrada'


    input_dir = '.'

    files = glob.glob('*.jpg')
    files += glob.glob('*.JPG')

    pm = PhotoManager()

    for f in files:
        pm.proccess_photo(f)

    pm.cleanup_photos()

    sys.exit(0)

References