本文实例讲述了python让图片按照exif信息里的创建时间进行排序的方法。分享给大家供大家参考。具体分析如下:
我们经常会从不同的设备里取出照片,比如照相机,手机,iphone等等,操作系统记录的创建日期经常 会因为拷贝等原因变动,下面的代码可以给图片按照exif里的创建时间进行排序,非常有用。
复制代码 代码如下:
import os
import shutil
import Image
from PIL.ExifTags import TAGS
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程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]