472,353 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Batch photo renaming with FTP interaction

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:


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:


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
  3. import EXIF
  4. import sys
  5. import os
  6. import string
  7. import datetime
  8. import ftplib
  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'])
  19. def format_exif_date(date):
  20.    name = date.replace(':', '')
  21.    name = name.replace(' ', '')
  22.    return name
  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)
  63. def valid_image(filename):
  64.    return string.lower(os.path.splitext(filename)[1]) in ['.jpg', '.jpeg']
  66. # MAIN LOOP
  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()
  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()
  81. ftp = ftplib.FTP("www.****.******.ca")
  82. ftp.login('******', '******')
  84. for filename in files:
  85.    rename_image(filename)
  87. ftp.quit()
Nov 21 '08 #1
0 1951

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

Similar topics

by: Eric McDaniel | last post by:
I am trying to read in a bunch of images and manipulate them using Image::Magick, then display them using Tk::Photo. I would like to do this...
by: David Cho | last post by:
I am still struggling with this. Used the PrintDocument class, but it was printing gobblygook. Does not recognize the PDF files as Acrobat files...
by: MLH | last post by:
I have a batch file named GetConf.bat. It contains a line like this: ipconfig /all >c:\MyAppDir\IPdata.txt I believe I could run the line with...
by: kaushikgscv | last post by:
Hi I want to create a batch file using C++ for installing the softwares in batch mode. eg: If i have a softeware -I start the start.exe--then...
by: abspring | last post by:
Hi I am a technology teacher/support person at an elementary school. I have created a database in which I need to insert a picture for each of our...
by: Shane | last post by:
I'm writing a program for renaming my picture files, and I want to use the picture time stamp as a prefix to the file names. I can get the time...
by: cumupkid | last post by:
II am trying to create a form that will allow me to upload photos to a folder in the site root directory and add the information to the mysql db at...
by: SPE - Stani's Python Editor | last post by:
Phatch is a simple to use cross-platform GUI Photo Batch Processor Phatch handles all popular image formats and can duplicate (sub)folder...
by: =?Utf-8?B?S3Jpc2huYWthbnRo?= | last post by:
I have got a requirement as follows. I am having a table RCBL_ERROR in a database in DB2. I need to create a batch file, which has to extract...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.