用脚本将本地照片库批量导入到Day One中

因为目前iCloud 空间已经不足,其中95%都是照片,之前入手了DayOne,且空间没有限制,订阅费一年也不少,再加上DayOne作为一款日记App 也比较有名,功能方面最大的就是地理视图与照片视图,尤其是去年今日,平常用来记记东西比较方便,目前支持语音,暂时还不支持视频,上次发了封建议邮件,答复目前正在测试阶段,下一个大版本就支持,另一个不足的是不支持模板,比较背景,彩色字体,开发者答复目前没有这个计划。

工具

  1. python+Anaconda+PyCharm(也是学习python一个练手机会)
  2. magick (图片处理,不丢失exif信息)
  3. exiftool (提取照片exif信息,插入到Dayone中,尤其是地理位置与创建时间)
  4. pandas (解析csv)
  5. shutil (文件复制)

流程如下:

效果如下

导入DayOne中,时间比较长,因为没有使用多线程(不会)

问题

  1. 取到的GPS 坐标是度分秒转换成x.xxx后,位置有一定的偏移,可能是地图导致
  2. 部分照片没有exif信息,只有先按上海的坐标和今天的创建时间导入后,到时候手动修改
  3. 苹果的heic格式的照片在转换时,失败,查了下magick是支持读取,但是报错,暂时没有处理

与Alfred 结合

  1. magick在转换单张照片时速度比较快,到时候写个脚本配合Alfred 做一个照片处理,很多时候在写文档时都需要贴图,在Mac上截图的图片通常比较大,目前用imageOptim,单张图片处理时间比较长。

代码

# -*- coding: utf-8 -*-

import os
import shutil
import pandas as pd



#分离文件
def separateImgByType(path, targetPath, type=[".mp4", ".mov"]):

    if not os.path.exists(targetPath):
        os.makedirs(targetPath)

    for obj in os.listdir(path):

        if obj == '.DS_Store': continue

        suffix = os.path.splitext(os.path.join(sourcePath, obj))[1].lower()

        if suffix in type:
            shutil.move(os.path.join(path, obj), os.path.join(targetPath, obj))


#删除文件名空格
def delfileNameSpace(path):
    for obj in os.listdir(path):
        if " " in obj:
            os.rename(os.path.join(path, obj), os.path.join(path, obj.replace(" ", "~")))



def convertImg(path, targetPath, size, rate=60):

    if not os.path.exists(targetPath):
        os.makedirs(targetPath)

    rateStr = str(rate) +"%";

    for obj in os.listdir(path):

        if obj == '.DS_Store': continue

        objSize = os.path.getsize(os.path.join(path, obj))

        if objSize <= size:
            shutil.copyfile(os.path.join(path, obj), os.path.join(targetPath, obj))
            continue;


        srcfrom = os.path.join(path, obj)

        srcto = os.path.join(targetPath, obj)

        cmd = "magick convert -resize %s %s %s" % (rateStr, srcfrom, srcto)

        print(cmd)

        os.system(cmd)

def createExifInfo(path):
    csvpath = os.path.join(path, "exifInfo.csv")
    cmd = "exiftool -f -r -p '$filename,$CreateDate,$GPSLatitude,$GPSLongitude,$ImageSize,$LensModel' %s > %s" % (path, csvpath)
    os.system(cmd)


def getExifInfo(path):

    list_data = pd.read_csv(path).to_records()

    return list_data

def convertGeo(geo,type):
    GPSLatitudList = str(geo).split("'")
    d = 0
    m = 0
    s = 0
    d = int(GPSLatitudList[0] if GPSLatitudList[0] != 0 else 0)
    if len(GPSLatitudList) == 2:
        m = int(GPSLatitudList[1] if GPSLatitudList[1] != 0 else 0)
    if len(GPSLatitudList) == 3:
        d = int(GPSLatitudList[2] if GPSLatitudList[2] != 0 else 0)

    dmd = d + m / 60.0 + d / 3600.0

    if dmd == 0.0 and type == 'Latitud': #121.549927,31.277549
        return 31.277549
    elif dmd == 0.0 and type == 'Logitud':
        return 121.549927
    else:
        return dmd


if __name__ == '__main__':

    size = 1.5 * 1024 * 1024

    sourcePath = "/Users/[xxxx]/Desktop/photo/"

    compactPath = "/Users/[xxxx]/Desktop/compactTarget"

    videoPath = "/Users/[xxxx]/Desktop/video/"

    HEICPath = "/Users/[xxxx]/Desktop/heic/"

    #separateImgByType(sourcePath, videoPath);

    #separateImgByType(sourcePath, HEICPath, (".HEIC"));

    #delfileNameSpace(sourcePath)

    #convertImg(sourcePath, compactPath, size)

    #createExifInfo(compactPath)

    listdata = getExifInfo(os.path.join(compactPath, "exifInfo.csv"))

    for obj in listdata:
        cmd = "dayone2 new '[%s]' '[%s]' -p '/Users/Spring/Desktop/photo1/%s' -d '%s' -j Import -coordinate %f %f"
        filename = obj[1]
        CreateDate = str(obj[2]) if str(obj[2]) != 'nan' else '2019-07-20 09:00:00'
        GPSLatitud = str(obj[3]) if str(obj[3]) != 'nan' else 0 #纬度
        GPSLogitud = str(obj[4]) if str(obj[4]) != 'nan' else 0 #经度
        ImageSize = obj[5]
        LensModel = obj[6]

        #print(CreateDate)

        title = '%s %s' % (filename, CreateDate)

        #print(GPSLatitud,"===",GPSLogitud)

        lt = convertGeo(GPSLatitud, "Latitud")

        lg = convertGeo(GPSLogitud, "Logitud")

        content = '%s ---- %s' % (ImageSize, LensModel)

        CreateDateList = str(CreateDate).split(" ")
        CreateDateList[0] = CreateDateList[0].replace(":","-");
        CreateDate = CreateDateList[0]+" "+CreateDateList[1]

        cmd = cmd % (title,content,filename,str(CreateDate),lt, lg)

        print(cmd)

        os.system(cmd)

转载于:https://my.oschina.net/SpringZhang/blog/3076832

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值