import os
import pickle
import time
# 解析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"):
print(photo.split("IMG_")[1].split(".jpg")[0].isdigit())
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
# 照片重命名
def renamePhoto(old, new, path):
os.rename(os.path.join(path, old), os.path.join(path, new))
return new in os.listdir(path)
folder_path = "C:\\Users\\96097\\Desktop\\img"
new_photo_names = parseConfig()
left_photo_names = pickle.loads(pickle.dumps(new_photo_names))
delete_record = []
rename_record = {}
while len(left_photo_names):
night_photos = getPhotos(folder_path)
time.sleep(0.5)
morning_photos = getPhotos(folder_path)
diff_n2m = getDifference(night_photos, morning_photos)
diff_m2n = getDifference(morning_photos, night_photos)
#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, folder_path):
print("is new photo")
if len(delete_record):
phot_name = delete_record[len(delete_record)-1]
delete_record.remove(phot_name)
else:
phot_name = left_photo_names[len(left_photo_names)-1]
left_photo_names.remove(phot_name)
if renamePhoto(i, phot_name, folder_path):
rename_record[phot_name] = i
print("renamed:[%s] to [%s]"%(i, phot_name))
elif len(diff_m2n) and (not len(diff_n2m)):
for i in diff_m2n:
delete_record.append(i)
print("delete:%s"%i)
print(rename_record)
最后修改:2024 年 04 月 21 日
© 允许规范转载