import os
import pickle
import time
import threading

config_file = "C:\\Users\\96097\\Desktop\\test\\config.txt"
photo_folder_path = "C:\\Users\\96097\\Desktop\\img"

# 解析config.txt文件,生成照片名
def parseConfig():
    #config_file = "C:\\Users\\96097\\Desktop\\test\\config.txt"
    config_content = open(config_file, 'r').read().split("\n")
    config = config_content[0:4]
    phone_numbers = config_content[4:len(config_content)]
  
    if config[2] == "corner":
        shoot_area = ["FP4", "FP9", "FP13", "FP18"]
    else:
        shoot_area = config[2].split("+")

    new_photo_names = []
    for num in phone_numbers:
        for area in shoot_area:
            new_photo_names.append(config[0] + "_" + config[3] + "_" + str(num) + "_" + config[1] + "_" + area + ".jpg")
        if config[2] == "corner":
            shoot_area.reverse()

    new_photo_names.reverse()

    return new_photo_names

# 获取文件夹内的照片
def getPhotos(path):
    photos = sorted(filter(lambda x:x.endswith(".jpg"), os.listdir(path)), key=lambda x:os.path.getctime(os.path.join(path, x)), reverse=True)
    return photos

# 照片是否在两秒内创建的
def isNewPhoto(photo, path):
    if photo.startswith("IMG_") and photo.endswith(".jpg"):
        if photo.split("IMG_")[1].split(".jpg")[0].isdigit() and time.time() - os.path.getctime(os.path.join(path, photo)) < 2:
            return 1
    return 0

# 获取文件夹的不同之处
def getDifference(a, b):
    difference = []
    for i in b:
        if i not in a:
            difference.append(i)
    return difference

new_photo_names = parseConfig()
left_photo_names = pickle.loads(pickle.dumps(new_photo_names))
delete_record = []
rename_record = {}

# 照片重命名
def renamePhoto(old, new, path):
    os.rename(os.path.join(path, old), os.path.join(path, new))
    while True:
        if new in os.listdir(path):
            rename_record[new] = old
            left_photo_names.remove(new)
            print("renamed:[%s] to [%s]"%(old, photo_name))
            break
  
while len(left_photo_names):
    night_photos = getPhotos(photo_folder_path)
    time.sleep(0.5)
    morning_photos = getPhotos(photo_folder_path)

    diff_n2m = getDifference(night_photos, morning_photos)
    diff_m2n = getDifference(morning_photos, night_photos)
    diff_m2n.reverse()

    #print(diff_n2m, diff_m2n)

    # 只增处理
    if len(diff_n2m) and (not len(diff_m2n)):
        for i in diff_n2m:
            #print("only: diff_n2m")
            if isNewPhoto(i, photo_folder_path):
                #print("is new photo")
                photo_name = left_photo_names[len(left_photo_names)-1]   
                threading.Thread(target=renamePhoto(i, photo_name, photo_folder_path)).start()   

    # 只删处理
    elif len(diff_m2n) and (not len(diff_n2m)):
        for i in diff_m2n:
            left_photo_names.append(i)
            print("delete:[%s]"%i)
    #print(rename_record)
  
最后修改:2024 年 05 月 13 日