美文网首页
通过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自动整理照片

    背景 随着照片越来越多,照片整理变成了个麻烦事。网上找整理照片工具试了几个,不好用和慢就不说了竟然还要收费(某款需...

  • 第七次分享06.28 接口测试(Python)

    听了五娃的接口测试&通过Python实现接口自动化测试,知识梳理整理如下: 1.接口测试,根据自己所在公司的实际情...

  • Python 学习笔记 117

    Python 模拟签到 需求 原计划需求是通过python自动模拟实现微信内一个社区的自动签到~ 满签有视频会员和...

  • python发送邮件库yagmail

    出处:虫师python自动发邮件库yagmail 一般发邮件方法 我以前在通过Python实现自动化邮件功能的时候...

  • 2019.1.24

    今日科技营照片整理及上传,视频制作完成 近几日其他营照片最迟明天整理完成上传,优化照片存放 支付渠道审核流程通过,...

  • Python 自动整理 Excel 表格

    相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作。最...

  • Python 自动整理 Excel 表格

    相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作。最...

  • Python 自动整理 Excel 表格

    相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作。最...

  • python批量自动整理文件

    阅读全文[https://mp.weixin.qq.com/s?__biz=MzA3ODk1Mzg0Mg==&mi...

  • 五大自动化测试的Python框架

    五大自动化测试的Python框架 作者:陈峻编译来源:51CTO 本文通过介绍与比较五种自动化测试的Python框...

网友评论

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

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