Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A Python script to organize digital photos UNIX

I tried different ways of organizing my digital photographs and eventually I came to a conclusion that the easiest one for me is to keep all my photos in one directory. To put them all in order I give each of them a filename which looks like this:

yymmdd-title-num.jpg

Example:

030224-trip-to-boston-033.jpg

I found that now I can find the right photograph in a matter of seconds even if I have thousands of them! First I download photos from my camera to my Pictures directory. Second I run the Python script shown below to rename all of them and move to my photo collection.

To make the script to work with Jaguar:
  1. Name the script; for example, 'mypics'
  2. Copy it to ~/bin directory
  3. Type chmod +x ~/bin/mypics to make it executable
  4. Modify the directories in the script
  5. Run it
#!/usr/bin/env  python

import os, sys, time

#Change these directories to your own settings
picturesDownloadDirectory="/Users/dimus/Pictures"
newPhotosDirectory="/Users/dimus/newPhotos"

#Don't change anything below this line:
#______________________________________
os.chdir(picturesDownloadDirectory)
files=os.listdir(os.curdir)
pics=[]

for i in files:
  if i [-3:].upper()=="JPG":
    pics.append(i)

print "Enter the name of the pictures: ",
name=raw_input()
print "nThere are %s pictures in your Pictures directory to move"

local_time=time.localtime(time.time())
year=str(local_time[0])[2:]
month=str(local_time[1])
if len(month)
[robg adds: I have not tested this script...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,374 views]  

A Python script to organize digital photos | 15 comments | Create New Account
Click here to return to the 'A Python script to organize digital photos' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A Python script to organize digital photos
Authored by: bluehz on Apr 02, '03 10:41:32AM
I have not tested this - but I was wondering... you wrote:

First I download photos from my camera to my Pictures directory. Second I run the Python script shown below to rename all of them and move to my photo collection. Yet in your script you clearly have a newPhotos and pictures variable. I am assuming that you import photos first into the newPhotos location and then the script moves the photos to the myPictures location. Is this correct? Just wondering about the wording above as it sounds like you imported from your camera to your PIctures directory which is what the default import of OS X does - but I think you meant to say import into "newPhotos"

Just so no one gets confused ;)

[ Reply to This | # ]

A Python script to organize digital photos
Authored by: dimus on Apr 02, '03 02:44:10PM

At first pictures are downloaded by default to ~/Pictures,
then I move them to some directory using the script.

The script should be customized and directories should be changed to correpsond to user's directories.

Hope this will clear it a bit

Also somehow the script became truncated in the hint, so I will post its full text in comments



[ Reply to This | # ]
A Python script to organize digital photos
Authored by: aubreyapple on Apr 02, '03 11:32:55AM

It looks like the script is incomplete, was truncated by hints, or I do not know what I am doing. It seems to end with:

if len(month)



[ Reply to This | # ]
A Python script to organize digital photos
Authored by: dimus on Apr 02, '03 02:37:11PM

Yes, you are right! Somehow the script got truncated, I'll try again here



_____START_OF_THE_SCRIPT_BELOW_THIS_LINE_____
#!/usr/bin/env python

import os, sys, time

#Change these directories to your own settings
picturesDownloadDirectory="/Users/dimus/Pictures"
newPhotosDirectory="/Users/dimus/newPhotos"


#Don't change anything below this line:
#_________________________________________________________
os.chdir(picturesDownloadDirectory)
files=os.listdir(os.curdir)
pics=[]

for i in files:
if i [-3:].upper()=="JPG":
pics.append(i)

print "Enter the name of the pictures: ",
name=raw_input()

local_time=time.localtime(time.time())
year=str(local_time[0])[2:]
month=str(local_time[1])
if len(month)<2: month='0'+ month
day=str(local_time[2])
if len(day)<2: day='0' + day
timestring=year+month+day
count=0
for i in pics:
count=count+1
num=''
if count<10:
num="00"+str(count)
elif count<100:
num="0"+str(count)
else:
num=str(count)
name=name.replace(' ','_')
file="%s/%s-%s-%s.%s" % (newPhotosDirectory,timestring,name,num,i[-3:].lower())
print "%s is moving" % file
os.system("mv %s %s" % (i,file))


#_____END_OF_THE_SCRIPT_____



[ Reply to This | # ]
Oups, script is truncated! Here it is again
Authored by: dimus on Apr 02, '03 02:54:24PM

#!/usr/bin/env python

import os, sys, time

#Change these directories to your own settings
picturesDownloadDirectory="/Users/dimus/Pictures"
newPhotosDirectory="/Users/dimus/newPhotos"


#Don't change anything below this line:
#_________________________________________________________
os.chdir(picturesDownloadDirectory)
files=os.listdir(os.curdir)
pics=[]

for i in files:
    if i [-3:].upper()=="JPG":
        pics.append(i)

print "Enter the name of the pictures: ",
name=raw_input()

local_time=time.localtime(time.time())
year=str(local_time[0])[2:]
month=str(local_time[1])
if len(month)


[ Reply to This | # ]
Oups, script is truncated! Here it is again
Authored by: dimus on Apr 02, '03 02:57:26PM

#!/usr/bin/env python

import os, sys, time

#Change these directories to your own settings
picturesDownloadDirectory="/Users/dimus/Pictures"
newPhotosDirectory="/Users/dimus/newPhotos"


#Don't change anything below this line:
#_________________________________________________________
os.chdir(picturesDownloadDirectory)
files=os.listdir(os.curdir)
pics=[]

for i in files:
    if i [-3:].upper()=="JPG":
        pics.append(i)

print "Enter the name of the pictures: ",
name=raw_input()

local_time=time.localtime(time.time())
year=str(local_time[0])[2:]
month=str(local_time[1])
if len(month)<2: month='0'+ month
day=str(local_time[2])
if len(day)<2: day='0' + day
timestring=year+month+day
count=0
for i in pics:
    count=count+1
    num=''
    if count<10:
        num="00"+str(count)
    elif count<100:
        num="0"+str(count)
    else:
        num=str(count)
    name=name.replace(' ','_')
    file="%s/%s-%s-%s.%s" % (newPhotosDirectory,timestring,name,num,i[-3:].lower())
    print "%s is moving" % file
    os.system("mv %s %s" % (i,file))

#This is the end  



[ Reply to This | # ]
A Python script to organize digital photos
Authored by: macman13 on Apr 02, '03 04:05:25PM
print "Enter the name of the pictures: ", name=raw_input()

Could be minimized to:
name = raw_input("Enter name of the pictures: ")

Just my $0.02 worth:)
SA

---
\\"I can do everything on my Mac I used to do on my PC, plus alot more ...\\"
--Me

[ Reply to This | # ]

A Python script to organize digital photos
Authored by: joestalin on Apr 02, '03 05:06:42PM

FWIW, GraphicConverter has very powerful renaming capabilities that can do much (all?) of this.

I keep my photos organized in subdirectories by occasion or subject (and with titles like "eventname YYMMDD")--GC's renaming features make it a lot easier to assign meaningful names to a lot of files at once.



[ Reply to This | # ]
A Python script to organize digital photos
Authored by: bluehz on Apr 02, '03 05:30:23PM

Semi-related - but I liek to proclaim my joy with wherever possible - even though I am unrelated to the project. For anyone dealing with lots of photos, images, etc - take a look ta the Gallery software. I have posted a Hint here for more info ion installing it.

http://gallery.menalto.com/modules.php?op=modload&name=News&file=index



[ Reply to This | # ]
A Python script to organize digital photos
Authored by: quentinsf on Apr 02, '03 05:41:30PM
For what it's worth, here's my version, which I've been using for some time, on Linux, then Windows, and now on my Mac, where I run it using an AppleScript menu item after Image Capture has put my photos in my Pictures directory.

This creates one directory per month and names the images within those based on their creation time, eg:
2003_03/2003_03_26-03_55_38.jpg
This means that I never lose the timestamp, even if I later manipulate the image using something which doesn't preserve times, tags etc.

Quentin


#!/usr/bin/python
# Copy image files from a source into a photo directory.
# Name them according to their date of creation.
# Put them in a directory based on the month.

import sys, os, glob, time, string

def copyfiles(srcdir, destdir, pattern, move=0):
    srcfiles = glob.glob(os.path.join(srcdir, pattern))
    for i in srcfiles:
        ext = os.path.splitext(i)[1]
        t = time.gmtime(os.path.getmtime(i))
        newname = time.strftime("%Y_%m_%d-%H_%M_%S",t) + ext
        monthdir = os.path.join(destdir, newname[:7])
        if not os.path.isdir(monthdir):
            print "Creating new dir", monthdir
            os.mkdir(monthdir)
        fullnewname = string.lower(os.path.join(monthdir,newname))

        # if file already exists, we add a suffix
        testname = fullnewname
        suffnum = 1
        while os.path.exists(testname):
                testname = "%s.%02d" % (fullnewname, suffnum)
                suffnum += 1
        if testname != fullnewname:
                fullnewname = testname

        print i,"->",fullnewname
        if move:
                os.rename(i, fullnewname)
        else:
                s = open(i,"rb")
                d = open(fullnewname, "wb")
                while 1:
                    b = s.read()
                    if not b: break
                    d.write(b)
                d.close()
                s.close()
    
def main():
    srcdir = "/Users/qsf/Pictures"
    destdir = "/Users/qsf/Pictures"
    if len(sys.argv) > 1:
        srcdir = sys.argv[1]
    if len(sys.argv) > 2:
        destdir = sys.argv[2]
    copyfiles(srcdir, destdir, "*.jpg*", move=1)
    copyfiles(srcdir, destdir, "*.JPG*", move=1)


if __name__ == "__main__":
    main()


[ Reply to This | # ]
A Python script to organize digital photos
Authored by: dimus on Apr 02, '03 10:13:59PM

I have trouble remembering what happened during this or that month, so I found that keeping all photos in one directory is better for me. Usually I organize a new directory when I have enough photos for a CD.



[ Reply to This | # ]
One more alternative - jhead
Authored by: haighy on Apr 03, '03 02:26:11AM

I use a program called jhead to rename my photos. It's written in C, so it's very fast...the link above has a pre-compiled OS X binary, but I built it from source with no problems.

The nice thing about this program is that it extracts image info from the EXIF header, so it's completely independent of the file's creation or modification time. Furthermore, it can reset the modification time of the actual JPEG to match that given in the EXIF metadata (in case you've rotated the pic in Photoshop, for example).

This one-liner renames an image in the form YYYY-MM-DD_hh-mm-ss, so that 'View by name' will list all files in chronological order, accurate to the second:

jhead -nf%Y-%m-%d_%H-%M-%S [files...]

Now cataloging those lurching, blurred, drunken mugshots is a breeze.



[ Reply to This | # ]
One more alternative - jhead
Authored by: dimus on Apr 03, '03 11:35:03AM

This is a great idea to use exif data for the date.
I'll try to figure out how to parse out the date from exif. If i'll succeed I'll post an updated script.



[ Reply to This | # ]
Use embeded IPTC creation time instead ?
Authored by: androse on Apr 03, '03 04:59:38AM

This is a good simple idea, but why not use the embeded IPTC infomation of the images themselves for the creation date for example. That way you get the date of the actual shooting, not the date of the transfer to the Mac.

I don't know if or how you can do it in Python, but in PHP you can use the iptcparse() function etc.

Time to learn Python :)



[ Reply to This | # ]
Use embeded IPTC creation time instead ?
Authored by: dimus on Apr 03, '03 10:53:04AM

This is a pretty good idea! AFAIK there is an IPTC parser in Python Image Library, but that makes the script dependent on a nondefault module; PIL is not a part of the Jaguar python distro and should be downloaded/copiled/installed separately.



[ Reply to This | # ]