注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python让图片按照exif信息里的创建时间进行排序

python 水墨上仙 2899次浏览 已收录 手机上查看

我们经常会从不同的设备里取出照片,比如照相机,手机,iphone等等,操作系统记录的创建日期经常 会因为拷贝等原因变动,下面的代码可以给图片按照exif里的创建时间进行排序,非常有用。

import os
import shutil
import Image
from PIL.ExifTags import TAGS
# code from http://www.75271.com
def print_all_known_exif_tags():
    for k in sorted(TAGS):
        print k, TAGS[k]

def print_all_exif_tags(image):
    try:
        img = Image.open(image)
    except Exception, e:
        print image, "skipping due to", e
    else:
        xf = img._getexif()
        for tag in xf:
            print TAGS.get(tag), xf[tag]
    finally:
        print 'done'


def get_minimum_creation_time(exif_data):
    mtime = "?"
    if 306 in exif_data and exif_data[306] < mtime: # 306 = DateTime
        mtime = exif_data[306]
    if 36867 in exif_data and exif_data[36867] < mtime: # 36867 = DateTimeOriginal
        mtime = exif_data[36867]
    if 36868 in exif_data and exif_data[36868] < mtime: # 36868 = DateTimeDigitized
        mtime = exif_data[36868]
    return mtime


def get_creationdate_with_filename_as_dict(list_of_folders):
    print "Processing all image files in:"

    result = {}
    for folder in list_of_folders:
        print "- " + folder
        counter = 0
        for f in os.listdir(folder):
            counter += 1
            fullFileName = folder + "\\" + f
            try:
                img = Image.open(fullFileName)
            except Exception, e:
                print "    Skipping '%s' due to exception: %s"%(f, e)
                continue
            mtime = get_minimum_creation_time(img._getexif())
            i = 0
            while mtime+"_"*i in result:
                i += 1
            mtime = mtime+"_"*i
            result[mtime] = fullFileName
        print "  Found %s orignal files in %s."%(counter, folder)
    print "Added total of %s to dictionary."%len(result)
    return result


def copy_from_image_dict_to_directory(image_dict, output_dir):
    assert os.path.exists(output_dir)
    for i,key in enumerate(sorted(image_dict)):
        dummy, extension =  os.path.splitext(image_dict[key])
        new_file_name = key.replace(":", "-") + extension
        output_file = output_dir + new_file_name
        shutil.copy2(image_dict[key], output_file)
    print "Copied %s files to %s"%(i+1, output_dir)


if __name__=="__main__":
    source_dir = "/var/tmp/images"
    output_dir = "/var/tmp/output"

    # obtain /var/tmp/images/iPhone, /var/tmp/images/CanonPowerShot, /var/tmp/images/Nikon1
    list_of_folders = [source_dir + subfolder for subfolder in os.listdir(source_dir)]

    all_files = get_creationdate_with_filename_as_dict(list_of_folders)
    copy_from_image_dict_to_directory(all_files, output_dir)


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python让图片按照exif信息里的创建时间进行排序
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……