-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoCleanup.py
182 lines (163 loc) · 6.86 KB
/
PhotoCleanup.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#-------------------------------------------------
# Photo Cleanup Tool
#
# This python + QT tool has the user select where all their photos and videos are stored
# then analyzing as best it can to determine the date of the photos/videos, and then creates
# a new directory structure at the destination, which will be clean and cronological.
# Over more than a dozen years of sync'ing digital cameras, smartphones, and family shared images,
# some order is needed to this mess. Online backups are organized, but the original archive is what we
# have locally.
#
# This uses MediaDB for most of the underlying analysis and management of the files
# This file is primarily the UI component to it
#
# author - patrick shehane
# date - July 9 2017
#-------------------
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QLabel, QPushButton
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QScrollArea
from PyQt5.QtWidgets import QMainWindow, QAction
from PyQt5.QtCore import Qt
import os
import MediaDB
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.FileCounter = 0
self.DictSearchDirectories = {}
self.DirButtons = []
self.initUI()
def initUI(self):
print("launch")
addButton = QPushButton("Add Directories...")
self.directoriesFound = QLabel("0 directories")
searchButton = QPushButton("Search Files")
self.filesFound = QLabel("0 files")
self.outputText = QLabel("")
self.outputScroll = QScrollArea()
self.outputScroll.setFixedHeight(100)
self.outputScroll.setWidgetResizable(True)
self.outputScroll.setWidget(self.outputText)
self.selectedDirectories = QLabel("Click to remove search directories")
analyzeButton = QPushButton("Analyze")
self.resultsText = QLabel("")
self.resultsScroll = QScrollArea()
self.resultsScroll.setFixedHeight(400)
self.resultsScroll.setWidgetResizable(True)
self.resultsScroll.setWidget(self.resultsText)
h_box = QHBoxLayout()
h_box.addWidget(addButton)
h_box.addWidget(self.directoriesFound)
h_box.addStretch()
h_box.addWidget(searchButton)
h_box.addWidget(self.filesFound)
h_box.addWidget(analyzeButton)
h_box.addStretch()
v_box = QVBoxLayout()
v_box.addLayout(h_box)
v_box.addWidget(self.selectedDirectories)
h2_box = QHBoxLayout()
v_box.addLayout(h2_box)
self.listDirButtonsBox = QVBoxLayout()
h2_box.addLayout(self.listDirButtonsBox)
h2_box.addStretch()
h2_box.addStretch()
v_box.addWidget(self.outputScroll)
v_box.addWidget(self.resultsScroll)
v_box.addStretch()
self.setLayout(v_box)
addButton.clicked.connect(self.addButtonClicked)
searchButton.clicked.connect(self.searchButtonClicked)
analyzeButton.clicked.connect(self.analyzeButtonClicked)
self.show()
def addButtonClicked(self):
# get the directory
file = str(QFileDialog.getExistingDirectory(self, "Add Directory..."))
state = self.DictSearchDirectories.get(file, 0)
if (state == 0):
self.DictSearchDirectories[file] = 1
self.directoriesFound.setText(str(len(self.DictSearchDirectories)) + " directories")
# visually add the directory, button used so removal easy
b = QPushButton(file)
self.DirButtons.append(b);
b.clicked.connect(self.removeDirButton)
self.listDirButtonsBox.addWidget(b)
else:
print("Directory already in the list")
def removeDirButton(self):
# get the reference and remove. but is it still in my list?
b = self.sender()
del self.DictSearchDirectories[b.text()]
self.listDirButtonsBox.removeWidget(b)
b.deleteLater()
b = None
# update visual counter
self.directoriesFound.setText(str(len(self.DictSearchDirectories)) + " directories")
self.outputText.setText("")
def searchButtonClicked(self):
self.FileCounter = 0
for theDir in self.DictSearchDirectories.keys():
for root, directories, files in os.walk(theDir):
for filename in files:
MediaDB.AddFileToDB(filename, root)
self.FileCounter += MediaDB.StatsDB["Total files"];
self.filesFound.setText(str(self.FileCounter) + " files")
self.filesFound.setText(str(self.FileCounter) + " files")
def analyzeButtonClicked(self):
self.analyzeFiles()
def analyzeFiles(self):
#self.outputText.setText(outstring)
MediaDB.UpdateDB()
MediaDB.CreateRecommendedTree()
self.resultsText.setText(MediaDB.GetRecommendedTreeString())
def saveFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
options |= QFileDialog.ShowDirsOnly
fileName, _ = QFileDialog.getSaveFileName(self,"QFileDialog.getSaveFileName()","","All Files (*);;Text Files (*.txt)", options=options)
return fileName
def openFileNamesDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
#options |= QFileDialog.ShowDirsOnly
files, _ = QFileDialog.getSaveFileNames(self,"Directories", "","All Files (*)", options=options)
return files
class PhotoCleanupApp(QMainWindow):
def __init__(self):
super().__init__()
self.form_widget = MainWidget()
self.setCentralWidget(self.form_widget)
self.init_ui()
def init_ui(self):
self.title = 'Photo Library Cleanup'
self.left = 10
self.top = 100
self.width = 500
self.height = 700
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
#editMenu = mainMenu.addMenu('Options')
#helpMenu = mainMenu.addMenu('Help')
exitButton = QAction('Exit', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)
# Set window background color
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(p)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
# to help with debugging - i can set the default start directory
if (len(sys.argv) == 3):
os.chdir(sys.argv[2])
MediaDB.InitDB("", 4) # -- initialize the MediaDB
mainwindow = PhotoCleanupApp()
#MediaDB.CleanupDB() # -- cleanup the MediaDB - in case we later support restarting the context
sys.exit(app.exec_())