美文网首页
通过python自动整理照片

通过python自动整理照片

作者: o树林o | 来源:发表于2019-01-23 00:17 被阅读0次

    背景

    随着照片越来越多,照片整理变成了个麻烦事。网上找整理照片工具试了几个,不好用和慢就不说了竟然还要收费(某款需要273大洋),今晚抽了一点时间写了个py脚本实现照片自动整理

    目标

    将给定目录中和子目录中所有照片,先按照拍照设备,再按照日期存放。

    具体功能

    1. 用pillow库对照片中的exif信息进行解析,得到设备信息和照片拍摄日期
    2. 用os.path检查和创建文件夹
    3. 用shutil库复制文件

    代码:

    # -*- coding: utf-8 -*-
    import os
    import os.path
    import shutil
    import random
    from PIL import Image
    from PIL.ExifTags import TAGS
    
    
    def get_exif_and_arrange_img(imgfile,imgfilename,outdir):
        if (not os.path.isdir(outdir)):
            os.mkdir(outdir)
            
        try:
            img = Image.open(imgfile,"r")
        except:
            print("not an image file:" + imgfilename)
            videodir = outdir + '\\'+ 'VIDEOS'
            if (not os.path.isdir(videodir)):
                os.mkdir(videodir)
            newvideofilename = imgfilename[:imgfilename.rfind('.')] + '_' + str(random.randint(1,999)) + imgfilename[imgfilename.rfind('.'):]
            shutil.copyfile(imgfile,videodir + '\\' + newvideofilename)
        else:
            try:
                exif_data = img._getexif()
            except:
                print("cannot read exif:" + imgfilename)
                otherdir = outdir + '\\'+ 'OTHERS'
                if (not os.path.isdir(otherdir)):
                    os.mkdir(otherdir)
                newphotofilename = imgfilename[:imgfilename.rfind('.')] + '_' + str(random.randint(1,999)) + imgfilename[imgfilename.rfind('.'):]
                shutil.copyfile(imgfile,otherdir + '\\' + newphotofilename)
            else:
                if (exif_data):
                    device = ''
                    photodate = ''
                    fulldate = ''
                    for tag, value in exif_data.items():
                        #begin
                        decoded = TAGS.get(tag,tag)
                        #print('%s (%s) = %s' % (decoded, tag, value) )
                        if (decoded == 'Make'):
                            device += value + '_'
                        if (decoded == 'Model'):
                            device += value
                        if (decoded == 'DateTime'):
                            photodate = value.replace(':','')[0:6]
                            fulldate = value.replace(':','')
                        #end
                    #begin
                    device = device.replace("\0",'')
                    #device = device.replace("\32",'')
                    print(imgfile + '---' + device + '---' + photodate)
                    
                    devicedir = outdir + '\\' + device
                    if (not os.path.isdir(devicedir)):
                        os.mkdir(devicedir)
                    device_datedir = devicedir +  '\\' + photodate
                    if (not os.path.isdir(device_datedir)):
                        os.mkdir(device_datedir)
                    newphotofilename = fulldate  + '_' + str(random.randint(1,9999999)) + imgfilename[imgfilename.rfind('.'):]
                    shutil.copyfile(imgfile,device_datedir + '\\' + newphotofilename)
                    img.close()
                    #end
    
    rootdir = "D:\\workspace\\example"
    outdir = "D:\\workspace\\out"
    
    for parent,dirnames,filenames in os.walk(rootdir):
        for filename in filenames:
            imgfile = os.path.join(parent,filename)
            imgfilename = filename
            get_exif_and_arrange_img(imgfile,imgfilename,outdir)
            
    

    相关文章

      网友评论

          本文标题:通过python自动整理照片

          本文链接:https://www.haomeiwen.com/subject/lfudjqtx.html