Python Renamer
Código fuente
Necesita la librería EXIF.py que podrás encontrar aquí- #!/usr/bin/env python
- #
- # RenamerPy Version 1.0 - Rename large JPG file with EXIF or file time attributes
- #
- # User EXIF.py library form Ianaré Sévi at http://sourceforge.net/projects/exif-py-3/
- #
- # CopyLeft (c) 2013 Alex Bergonzini
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- #
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- #
- # 2. Redistributions in binary form must reproduce the above
- # copyright notice, this list of conditions and the following
- # disclaimer in the documentation and/or other materials provided
- # with the distribution.
- #
- # 3. Neither the name of the authors nor the names of its contributors
- # may be used to endorse or promote products derived from this
- # software without specific prior written permission.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- import os, time, sys, getopt, shutil
- import EXIF
- path=''
- path_d=''
- option_ym=True
- option_y=False
- option_m=False
- option_t=False
- #--[ Convertimos de numeros a dos posiciones
- def convert_hms(valor):
- tmp=str(valor)
- if valor<=9:
- tmp="0"+tmp
- return tmp
- #--[ Buscamos los ficheros por subdirectorios
- def ficheros(directorio):
- mat=[]
- fichero=[]
- for dirname, dirnames, filenames in os.walk(directorio):
- for filename in filenames:
- fichero=[]
- nom=os.path.join(dirname, filename)
- nom_up=nom.upper()
- if nom_up.find('JPG' or 'JPEG')>0:
- fichero.append(os.path.join(dirname,""))
- fichero.append(os.path.join(filename,""))
- fichero.append(os.path.join(dirname, filename))
- fecha=time.ctime(os.path.getmtime(os.path.join(dirname, filename)))
- print(fecha)
- fechas=time.strptime(fecha,'%a %b %d %H:%M:%S %Y')
- fichero.append(str(fechas[0])+convert_hms(fechas[1])+
- convert_hms(fechas[2])+convert_hms(fechas[3])+
- convert_hms(fechas[4])+convert_hms(fechas[5]))
- mat.append(fichero)
- return mat
- def hardwork(mi_dir,destino_dir):
- global option_m, option_y, option_ym, option_t
- lista=ficheros(mi_dir)
- for n in range(0, len(lista)):
- file=lista[n]
- f=open(file[2],"rb")
- try:
- tags=EXIF.process_file(f)
- encontrado=False
- #--[ Buscamos las etiquetas
- if 'EXIF DateTimeOriginal' in tags:
- fecha=tags['EXIF DateTimeOriginal']
- encontrado=True
- if 'DateTimeOriginal' in tags:
- fecha=tags['DateTimeOriginal']
- encontrado=True
- if 'DateTime' in tags:
- fecha=tags['DateTime']
- encontrado=True
- if encontrado:
- fechas=str(fecha)
- fechas=fechas.replace(':','')
- fechas=fechas.replace(' ','')
- file[3]= fechas[2:-1]
- #Comprobamos si el fichero ya existe
- origen_f=file[2]
- real_destino=os.path.join(destino_dir,file[3][0:4])
- if not os.path.isdir(real_destino):
- os.mkdir(real_destino)
- real_destino=os.path.join(real_destino,"X")
- real_destino=real_destino[0:-1]
- real_destino=real_destino+file[3][0:6]
- if not os.path.isdir(real_destino):
- os.mkdir(real_destino)
- real_destino=os.path.join(real_destino,"X")
- real_destino=real_destino[0:-1]
- destino=real_destino+file[3]+'.jpg'
- x=0
- while os.path.isfile(destino)==True:
- x_tmp=convert_hms(x)
- destino = real_destino+file[3][0:-2]+x_tmp+'.jpg'
- x+=1
- print("Processing "+origen_f+"->"+destino)
- if option_t==True:
- if option_m==True:
- shutil.move(origen_f,destino)
- else:
- shutil.copy(origen_f,destino)
- finally:
- f.close()
- #hardwork('t:\\hola\\')
- def print_header():
- print("** RenamerPy v1.0 ** Easy Renamer for your photo collection "+
- "** CopyLeft 2013 Bergonzini at http://www.bergonzini.com\n\n")
- def print_help():
- print_header()
- print("Options: \n"+
- " -p Path with the photo collection to rename\n"+
- " -d Path to save the renamed photos\n"+
- " -Y Create in the destination folder subfolders like YYYY/YYYYMM/ \n"+
- " -y Like -Y option but only YYYY/ \n"+
- " -m Move files instead copy\n"+
- " -t Test mode don''t move/copy only\n")
- def command_line():
- global path, option_m, option_y, option_ym, option_t
- path_d=''
- try:
- opts, args = getopt.getopt(sys.argv[1:],"hp:d:yYmt")
- except getopt.GetoptError:
- print_help()
- for opt, arg in opts:
- if opt == '-h':
- print_help()
- exit()
- elif opt == "-p":
- path=arg
- elif opt == "-d":
- path_d=arg
- elif opt =="-Y":
- option_ym=True;
- elif opt == "-y":
- option_y=True
- elif opt == "-t":
- option_t=True
- elif opt == '-m':
- option_m=True;
- if not os.path.isdir(path):
- print_header()
- print("[ERROR] Origen path not found! "+path)
- else:
- if not os.path.isdir(path_d):
- os.mkdir(path_d)
- if path!="" and path_d!="":
- print_header()
- print("Processing "+path)
- hardwork(path,path_d)
- else:
- print_help()
- command_line()
(:sourcend:)