python使用PIL库提取图片的EXIF数据,并做重命名,,python使用PIL库


python使用PIL库提取图片的EXIF数据,并做重命名

# -*- coding: Windows-1251 -*-'''rename_to_exiftime.pyRename JPEG files according to EXIF-date using PILIf global variable CREATE_HARDLINK is set, script creates Windows (XP) batch file for creating hardlink version of source filesAuthor: Denis Barmenkov <denis.barmenkov@gmail.com>Modified by: Jorge Barnaby <jorge.barnaby@gmail.com>Improved rename process on duplicatesCopyright: this code is free, but if you want to use it,            please keep this multiline comment along with function source.            Thank you.2009-02-10 18:14 '''import Imageimport osimport reimport sysimport timeCREATE_HARDLINK=0def extract_jpeg_exif_time(jpegfn):    if not os.path.isfile(jpegfn):        return None    try:        im = Image.open(jpegfn)        if hasattr(im, '_getexif'):            exifdata = im._getexif()            ctime = exifdata[0x9003]            #print ctime            return ctime    except:         _type, value, traceback = sys.exc_info()        print "Error:\n%r", value    return Nonedef get_exif_prefix(jpegfn):    ctime = extract_jpeg_exif_time(jpegfn)    if ctime is None:        return None    ctime = ctime.replace(':', '')    ctime = re.sub('[^\d]+', '_', ctime)    return ctimedef rename_jpeg_file(fn):    if not os.path.isfile(fn):        return 0    ext = os.path.splitext(fn)[1].lower()    if ext not in ['.jpg', '.jpeg', '.jfif', '.nef', '.png']:        return 0    path, base = os.path.split(fn)    #print base # status    prefix = get_exif_prefix(fn)    if prefix is None:        print '%s could not be renamed' % (base)        return 0    if base.startswith(prefix):        print '%s file already renamed' % (base)        return 0 # file already renamed to this prefix    exists = True    index = 0    while exists:        if index == 0:            new_name = prefix + ext # + '_' + base        else:            new_name = prefix + '_' + str(index) + ext        new_full_name = os.path.join(path, new_name)        exists = os.path.isfile(new_full_name)        index = index + 1    if CREATE_HARDLINK:        f = open('CREATE_HARDLINK.cmd', 'a')        f.write('fsutil hardlink create "%s" "%s"\n' % (new_full_name, fn))        f.close()    else:        try:            os.rename(fn, new_full_name)            print '%s renamed to %s' % (base, new_name)         except:            print 'ERROR rename %s --> %s' % (fn, new_full_name)            return 0    return 1def rename_jpeg_files_in_dir(dn):    names = os.listdir(dn)    count=0    for n in names:        file_path = os.path.join(dn, n)        count += rename_jpeg_file(file_path)    return countif __name__=='__main__':    try:        path = sys.argv[1]    except IndexError:        print '''Usage:    rename_to_exiftime.py  filename.[jpeg|jpg|jfif]or  rename_to_exiftime.py  dirname'''        sys.exit(1)    if os.path.isfile(path):        rename_jpeg_file(path)    elif os.path.isdir(path):        count = rename_jpeg_files_in_dir(path)        print '%d file(s) renamed.' % count    else:        print 'ERROR: path not found: %s' % path

之前我从不同的朋友哪儿拿到一堆图片,每种相机的命名规则都不一样,因为我写了这个脚本来根据EXIF中记录的时间统一命名图片。

PIL 库下载地址: http://www.pythonware.com/products/pil/

评论关闭