Batch import of local photo galleries into Day One using scripts

Because iCloud currently has insufficient space, 95% of which are photos. DayOne was previously started with no space restrictions, and the subscription fee is not less than a year. DayOne is also well-known as a diary App. The most powerful features are the geographic and photo views, especially those used to remember things last year and today.Convenient, currently voice-enabled, not video-enabled yet, last sent a suggestion email, reply is currently in beta, next major version will support, another shortage is not support templates, background comparison, color fonts, developers reply currently do not have this plan.

tool

  1. Python + Anaconda + PyCharm (also a practice session for learning python)
  2. magick (Picture processing without losing exif information)
  3. exiftool (extract photo exif information and insert it into Dayone, especially geographic location and creation time)
  4. pandas (resolving csv)
  5. shutil (file copy)

The process is as follows:

The effect is as follows

Importing into DayOne took longer because multithreading was not used (no)

problem

  1. When the GPS coordinates are converted from degrees, minutes, seconds to x.x x x, the position is offset, which may be caused by the map
  2. Some photos do not have exif information, only import them according to Shanghai coordinates and today's creation time, then modify them manually
  3. Photos in Apple's heic format failed during conversion, checked that magick supports reading, but an error occurred and was not handled for the time being

Combining with Alfred

  1. magick converts individual photos very quickly. Write a script to do a photo processing with Alfred at that time. Many times you need to map when you write a document. The pictures you take on Mac are usually larger. Currently, with imageOptim, single pictures take longer to process.

Code

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

import os
import shutil
import pandas as pd



#Separate Files
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))


#Remove Filename Spaces
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 #latitude
        GPSLogitud = str(obj[4]) if str(obj[4]) != 'nan' else 0 #longitude
        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)

Keywords: Python less Anaconda Pycharm

Added by jess3333 on Sat, 20 Jul 2019 19:54:17 +0300