本文实例讲述了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程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]