By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Batch photo renaming with FTP interaction

P: 1
I'm trying to write a code that batch renames photos. In the end each photo should be named:

AABBB1111YYYYMM222 where

AABBB1111 defines a given place. (i.e. CAMTL = montreal and four digits define the area in Montreal.)

YYYYMM reveals the year and month that the photo was taken based on EXIF data.

222 allows for 999 photos of a given place in a given month.

That is to say, if there are three photos of the Olympic Stadium in Montreal taken in June 2008, the photos ought to be renamed to:

CAMTL8012200806001
CAMTL8012200806002
CAMTL8012200806003

BUT there is an FTP with a bunch of photos already uploaded. So say, hypothetically, someone had previously uploaded 3 photos of the Olympic Stadium in Montreal taken in June 2008. Then my pictures of the Olympic Stadium ought to be renamed to:

CAMTL8012200806004
CAMTL8012200806005
CAMTL8012200806006

This is where my program stops working. I can't figure out why my FTP interaction isn't working. I'm trying to search a folder in the FTP for pictures with the same first 15 digits as the file that's being renamed. If any exist, then I need to add 1 to the last three digits.

I'm kind of new to Python. Can anyone help me out? Here's my script:


Expand|Select|Wrap|Line Numbers
  1. # PHOTO RENAMER
  2.  
  3. import EXIF
  4. import sys
  5. import os
  6. import string
  7. import datetime
  8. import ftplib
  9.  
  10. # FUNCTION DEFINITIONS
  11.  
  12. def date_from_exif(file):
  13.    data = EXIF.process_file(file, details=False, debug=False)
  14.    if not data or not data.has_key('EXIF DateTimeOriginal'):
  15.       return False
  16.    else:
  17.       return str(data['EXIF DateTimeOriginal'])
  18.  
  19. def format_exif_date(date):
  20.    name = date.replace(':', '')
  21.    name = name.replace(' ', '')
  22.    return name
  23.  
  24. def rename_image(filename):
  25.    if not valid_image(filename):
  26.       return
  27.    try:
  28.       file = open(filename, 'rb')
  29.    except:
  30.       print "'%s': Cannot open for reading.\n" % filename
  31.       return
  32.    date = date_from_exif(file)
  33.    if (date == False):
  34.       return
  35.    file.close()
  36.    newname = dir[-9:] + format_exif_date(date)
  37.    ftpdir = ftp.nlst('testfolder/')
  38.    return ftpdir
  39.    same_month = list()
  40.    for item in ftpdir:
  41.       if item[:15] == newname:
  42.          same_month.append(item)
  43.       else:
  44.          return
  45.    return same_month
  46.    if same_month == []:
  47.       suffix = "001"
  48.    else:
  49.       suffix = same_month[-1]
  50.       suffix = float(suffix[16:18])
  51.       suffix = suffix + 1
  52.       suffix = suffix.zfill(3)
  53.       suffix = str(suffix)
  54.    return suffix
  55.    newname = newname + suffix + ".jpg"
  56.    if os.path.exists(newname):
  57.       print '[FAIL] ' + filename + ' already exists, ignoring...'
  58.       return
  59.    else:
  60.       print filename + ' -> ' + newname
  61.       os.rename(filename, newname)
  62.  
  63. def valid_image(filename):
  64.    return string.lower(os.path.splitext(filename)[1]) in ['.jpg', '.jpeg']
  65.  
  66. # MAIN LOOP
  67.  
  68. dir = raw_input("Enter directory for image name conversion\n")
  69. if not os.path.isdir(dir):
  70.    print '"' + dir + '" is not a directory. Exiting...'
  71.    exit()
  72.  
  73. os.chdir(dir)
  74. files = os.listdir('.')
  75. # confirm?
  76. confirm = raw_input('Process ' + str(len(files)) + ' files in directory "' + dir + '"? [y/n]: ')
  77. if string.lower(confirm) != 'y':
  78.    print 'Exiting...'
  79.    exit()
  80.  
  81. ftp = ftplib.FTP("www.****.******.ca")
  82. ftp.login('******', '******')
  83.  
  84. for filename in files:
  85.    rename_image(filename)
  86.  
  87. ftp.quit()
  88.  
Nov 21 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.