473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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:

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
0 2036

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

Similar topics

2
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 without creating a temp file for each image, since there...
3
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 and just prints text. Can't get the shell...
13
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 something like ShellWait by Terry Kreft. Any...
1
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 it ask me path--the some package to...
3
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 650+ students. I have a table of student...
2
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 stamp, but it is extraordinarily slow! I have about...
1
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 the same time. I have created two forms, one...
1
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 hierarchies. It can batch resize, rotate, apply...
1
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 the rows from this table and write those extracted...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.