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
- # PHOTO RENAMER
- import EXIF
- import sys
- import os
- import string
- import datetime
- import ftplib
- # FUNCTION DEFINITIONS
- def date_from_exif(file):
- data = EXIF.process_file(file, details=False, debug=False)
- if not data or not data.has_key('EXIF DateTimeOriginal'):
- return False
- else:
- return str(data['EXIF DateTimeOriginal'])
- def format_exif_date(date):
- name = date.replace(':', '')
- name = name.replace(' ', '')
- return name
- def rename_image(filename):
- if not valid_image(filename):
- return
- try:
- file = open(filename, 'rb')
- except:
- print "'%s': Cannot open for reading.\n" % filename
- return
- date = date_from_exif(file)
- if (date == False):
- return
- file.close()
- newname = dir[-9:] + format_exif_date(date)
- ftpdir = ftp.nlst('testfolder/')
- return ftpdir
- same_month = list()
- for item in ftpdir:
- if item[:15] == newname:
- same_month.append(item)
- else:
- return
- return same_month
- if same_month == []:
- suffix = "001"
- else:
- suffix = same_month[-1]
- suffix = float(suffix[16:18])
- suffix = suffix + 1
- suffix = suffix.zfill(3)
- suffix = str(suffix)
- return suffix
- newname = newname + suffix + ".jpg"
- if os.path.exists(newname):
- print '[FAIL] ' + filename + ' already exists, ignoring...'
- return
- else:
- print filename + ' -> ' + newname
- os.rename(filename, newname)
- def valid_image(filename):
- return string.lower(os.path.splitext(filename)[1]) in ['.jpg', '.jpeg']
- # MAIN LOOP
- dir = raw_input("Enter directory for image name conversion\n")
- if not os.path.isdir(dir):
- print '"' + dir + '" is not a directory. Exiting...'
- exit()
- os.chdir(dir)
- files = os.listdir('.')
- # confirm?
- confirm = raw_input('Process ' + str(len(files)) + ' files in directory "' + dir + '"? [y/n]: ')
- if string.lower(confirm) != 'y':
- print 'Exiting...'
- exit()
- ftp = ftplib.FTP("www.****.******.ca")
- ftp.login('******', '******')
- for filename in files:
- rename_image(filename)
- ftp.quit()