-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
95 lines (72 loc) · 3.86 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import sys
import numpy as np
import mediapipe as mp
import cv2 as cv
from pathlib import *
import argparse
import classes
import time
from tqdm import tqdm, trange
from PIL import Image as ExifImage
# if __name__ == "__main__": # today (11/24/23) i finally understood what this does when trying to use main as a package in terminal
parser = argparse.ArgumentParser(
prog='Daily Picture Aligner',
description = "Automate the eye coordinate alignment process (scaling, rotating, translating) of hundreds of photo to an initial image. ")
arguments = parser.parse_args()
###VARIABLES
DailyPhotoPath = "./DailyPhotos/"# "ALL Daily photos/"
BaseImagePath = "./BaseImages/" # "./BaseImages/"
OutputPath = "./AlignedPhotos/"
fileAffix = "_Aligned.jpg"
fileSuffix = "_"
class FileManager: #toDO: find a better classname for the filemanager
def __init__(self,file):
self.file
files = os.listdir(Path(BaseImagePath))
files = list(filter(lambda a: not a.startswith("."), files))
if len(files) > 1:
raise Exception("Ensure there's only one base image, and that you deleted the initial text file.")
file = files[0]
libfile = PurePath(BaseImagePath+file)
BaseImage = classes.BaseImage(libfile)
###RUNNING CODE
print("\n-------------------------------\nStarting Script\nAdjusting Images\nWriting Files\nCheck 'AlignedPhotos' folder and make sure the script is working\nCheck ErrorLog.txt if output isn't working as expected!\n-------------------------------\n")
time.sleep(3)
#The following code sorts images by EXIF data, the metadate embeded in the file, as opposed to creation date.
#This is because files downloaded from, for example, GooglePhotos, have creation date at the time of download, but exif data of the time they were taken
fileList = os.listdir(DailyPhotoPath)
exifDict = dict.fromkeys(fileList)
for img in fileList:
try:
exifDict[img] = (ExifImage.open(DailyPhotoPath+img)._getexif()[36867])
except:
del exifDict[img]
newList = sorted(exifDict.items(), key=lambda x: x[1]) #sorts images in a [(path/, date), (path, date)] format
fileListSorted = []
for img in newList:
img = Path(DailyPhotoPath + img[0])
fileListSorted.append(img)
completedFiles = [x.split(fileSuffix)[1] for x in os.listdir(OutputPath)]
count = 1
#this for gets all files keeps the ones that are uncorrupted (>0bytes) jpg/png files.
for file in tqdm(fileListSorted):
libfile = file
file = file.name #Pathlib returns it as a pathlib.WindowsPath instead of a string, and it returns the parent folder like this: parentfolder/file.jpg, so we need to convert it back into a string for the logic ahead using file.name, just the file name as string
if file in completedFiles: #if our file has already been aligned, do nothing.
count+=1
pass
else:
#endswith("g") because that's for png/jpg files. I didn't know how to check for the last 4 position slots because each fle name size is different and the initial start is different. anyways this works currently
if file.lower().endswith("g") and os.path.getsize(DailyPhotoPath + file) > 0:
currentImage = classes.Image(libfile)
success = currentImage.alignImagetoBaseImage(BaseImage)
if success:
cv.imwrite(OutputPath+str(count)+"_"+fileSuffix+libfile.stem+fileAffix , currentImage.cvimage)
count+=1
else:
pass #handling errors in classes.py
else:
pass
print("\nSuccessfully Aligned " + str(count) +" Pictures!\nIf you found this script useful, please let me know, I would love your feedback! \nIf you want to directly support my future (college, projects, etc.), my CashApp is $NoahCutz, or you can BuyMeACoffee (https://buymeacoffee.com/noahbuchanan).")
input("Press Enter to exit: ")