-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortimages_multiview.py
351 lines (317 loc) · 14.1 KB
/
sortimages_multiview.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# todo:
# Filename dupicate scanning to prevent collisions
# Check if filename already exists on move.
# implement undo
import os
from sys import exit
from shutil import move as shmove
import tkinter as tk
from tkinter.messagebox import askokcancel
from math import floor
import json
import random
from math import floor, sqrt
from tkinter import filedialog as tkFileDialog
import concurrent.futures as concurrent
import logging
from hashlib import md5
import pyvips
from gui import GUIManager, randomColor
class Imagefile:
path = ""
dest = ""
dupename=False
def __init__(self, name, path) -> None:
self.name = tk.StringVar()
self.name.set(name)
self.path = path
self.checked = tk.BooleanVar(value=False)
self.moved = False
def move(self) -> str:
destpath = self.dest
if destpath != "" and os.path.isdir(destpath):
file_name = self.name.get()
# Check for name conflicts (source -> destination)
exists_already_in_destination = os.path.exists(os.path.join(destpath, file_name))
if exists_already_in_destination:
print(f"File {self.name.get()[:30]} already exists at destination. Cancelling move.")
return ("") # Returns if 1. Would overwrite someone
try:
new_path = os.path.join(destpath, file_name)
old_path = self.path
# Throws exception when image is open.
shmove(self.path, new_path)
self.moved = True
self.show = False
self.guidata["frame"].configure(
highlightbackground="green", highlightthickness=2)
self.path = new_path
returnstr = ("Moved:" + self.name.get() +
" -> " + destpath + "\n")
destpath = ""
self.dest = ""
self.hasunmoved = False
return returnstr
except Exception as e:
# Shutil failed. Delete the copy from destination, leaving the original at source.
# This only runs if shutil fails, meaning the image couldn't be deleted from source.
# It is therefore safe to delete the destination copy.
if os.path.exists(new_path) and os.path.exists(old_path):
os.remove(new_path)
print("Shutil failed. Coudln't delete from source, cancelling move (deleting copy from destination)")
return "Shutil failed. Coudln't delete from source, cancelling move (deleting copy from destination)"
else:
logging.warning(f"Error moving/deleting: %s . File: %s {e} {self.name.get()}")
self.guidata["frame"].configure(
highlightbackground="red", highlightthickness=2)
return ("Error moving: %s . File: %s", e, self.name.get())
def setid(self, id):
self.id = id
def setguidata(self, data):
self.guidata = data
def setdest(self, dest):
self.dest = dest["path"]
logging.debug("Set destination of %s to %s",
self.name.get(), self.dest)
class SortImages:
imagelist = []
destinations = []
exclude = []
thumbnailsize = 256
def __init__(self) -> None:
self.hasunmoved=False
self.existingnames = set()
self.duplicatenames=[]
self.autosave=True
self.gui = GUIManager(self)
# note, just load the preferences then pass it to the guimanager for processing there
if(os.path.exists("data") and os.path.isdir("data")):
pass
else:
os.mkdir("data")
hotkeys = ""
# todo: replace this with some actual prefs manager that isn't a shittone of ifs
self.threads = 5
try:
with open("prefs.json", "r") as prefsfile:
jdata = prefsfile.read()
jprefs = json.loads(jdata)
if 'hotkeys' in jprefs:
hotkeys = jprefs["hotkeys"]
if 'thumbnailsize' in jprefs:
self.gui.thumbnailsize = int(jprefs["thumbnailsize"])
self.thumbnailsize = int(jprefs["thumbnailsize"])
if 'threads' in jprefs:
self.threads = jprefs['threads']
if "hideonassign" in jprefs:
self.gui.hideonassignvar.set(jprefs["hideonassign"])
if "hidemoved" in jprefs:
self.gui.hidemovedvar.set(jprefs["hidemoved"])
if "sortbydate" in jprefs:
self.gui.sortbydatevar.set(jprefs["sortbydate"])
self.exclude = jprefs["exclude"]
self.gui.sdpEntry.delete(0, len(self.gui.sdpEntry.get()))
self.gui.ddpEntry.delete(0, len(self.gui.ddpEntry.get()))
self.gui.sdpEntry.insert(0, jprefs["srcpath"])
self.gui.ddpEntry.insert(0, jprefs["despath"])
if "squaresperpage" in jprefs:
self.gui.squaresperpage.set(int(jprefs["squaresperpage"]))
if "geometry" in jprefs:
self.gui.geometry(jprefs["geometry"])
if "lastsession" in jprefs:
self.gui.sessionpathvar.set(jprefs['lastsession'])
if "autosavesession" in jprefs:
self.autosave = jprefs['autosave']
if len(hotkeys) > 1:
self.gui.hotkeys = hotkeys
except Exception as e:
logging.error("Error loading prefs.json, it is possibly corrupt, try deleting it, or else it doesn't exist and will be created upon exiting the program.")
logging.error(e)
self.gui.mainloop()
def moveall(self):
loglist = []
for x in self.imagelist:
out = x.move()
if isinstance(out, str):
loglist.append(out)
try:
if len(loglist) > 0:
with open("filelog.txt", "a") as logfile:
logfile.writelines(loglist)
except Exception as e:
logging.error(f"Failed to write filelog.txt: {e}")
self.gui.hidemoved()
def walk(self, src):
duplicates = self.duplicatenames
existing = self.existingnames
for root, dirs, files in os.walk(src, topdown=True):
dirs[:] = [d for d in dirs if d not in self.exclude]
for name in files:
ext = name.split(".")[len(name.split("."))-1].lower()
if ext == "png" or ext == "gif" or ext == "jpg" or ext == "jpeg" or ext == "bmp" or ext == "pcx" or ext == "tiff" or ext == "webp" or ext == "psd" or ext == "jfif":
imgfile = Imagefile(name, os.path.join(root, name))
if name in existing:
duplicates.append(imgfile)
imgfile.dupename=True
else:
existing.add(name)
self.imagelist.append(imgfile)
#Default sorting is based on name. This sorts by date modified.
if self.gui.sortbydatevar.get():
self.imagelist.sort(key=lambda img: os.path.getmtime(img.path), reverse=True)
return self.imagelist
def checkdupefilenames(self, imagelist):
duplicates: list[Imagefile] = []
existing: set[str] = set()
for item in imagelist:
if item.name.get() in existing:
duplicates.append(item)
item.dupename=True
else:
existing.add(item.name)
return duplicates
def setDestination(self, *args):
self.hasunmoved = True
marked = []
dest = args[0]
try:
wid = args[1].widget
except AttributeError:
wid = args[1]["widget"]
if isinstance(wid, tk.Entry):
pass
else:
for x in self.imagelist:
if x.checked.get():
marked.append(x)
for obj in marked:
obj.setdest(dest)
obj.guidata["frame"]['background'] = dest['color']
obj.guidata["canvas"]['background'] = dest['color']
obj.checked.set(False)
self.gui.hideassignedsquare(marked)
def savesession(self,asksavelocation):
if asksavelocation:
filet=[("Javascript Object Notation","*.json")]
savelocation=tkFileDialog.asksaveasfilename(confirmoverwrite=True,defaultextension=filet,filetypes=filet,initialdir=os.getcwd(),initialfile=self.gui.sessionpathvar.get())
else:
savelocation = self.gui.sessionpathvar.get()
if len(self.imagelist) > 0:
imagesavedata = []
for obj in self.imagelist:
if hasattr(obj, 'thumbnail'):
thumb = obj.thumbnail
else:
thumb = ""
imagesavedata.append({
"name": obj.name.get(),
"path": obj.path,
"dest": obj.dest,
"checked": obj.checked.get(),
"moved": obj.moved,
"thumbnail": thumb,
"dupename":obj.dupename
})
save = {"dest": self.ddp, "source": self.sdp,
"imagelist": imagesavedata,"thumbnailsize":self.thumbnailsize,'existingnames':list(self.existingnames)}
with open(savelocation, "w+") as savef:
json.dump(save, savef)
def loadsession(self):
sessionpath = self.gui.sessionpathvar.get()
if os.path.exists(sessionpath) and os.path.isfile(sessionpath):
with open(sessionpath, "r") as savef:
sdata = savef.read()
savedata = json.loads(sdata)
gui = self.gui
self.ddp = savedata['dest']
self.sdp = savedata['source']
self.setup(savedata['dest'])
if 'existingnames' in savedata:
self.existingnames = set(savedata['existingnames'])
for o in savedata['imagelist']:
if os.path.exists(o['path']):
n = Imagefile(o['name'], o['path'])
n.checked.set(o['checked'])
n.moved = o['moved']
n.thumbnail = o['thumbnail']
n.dupename=o['dupename']
n.dest=o['dest']
self.imagelist.append(n)
self.thumbnailsize=savedata['thumbnailsize']
self.gui.thumbnailsize=savedata['thumbnailsize']
listmax = min(gui.squaresperpage.get(), len(self.imagelist))
sublist = self.imagelist[0:listmax]
gui.displaygrid(self.imagelist, range(0, min(gui.squaresperpage.get(),listmax)))
gui.guisetup(self.destinations)
gui.hidemoved()
gui.hideassignedsquare(sublist)
else:
logging.error("No Last Session!")
def validate(self, gui):
samepath = (gui.sdpEntry.get() == gui.ddpEntry.get())
if((os.path.isdir(gui.sdpEntry.get())) and (os.path.isdir(gui.ddpEntry.get())) and not samepath):
self.sdp = gui.sdpEntry.get()
self.ddp = gui.ddpEntry.get()
logging.info("main class setup")
self.setup(self.ddp)
logging.info("GUI setup")
gui.guisetup(self.destinations)
gui.sessionpathvar.set(os.path.basename(
self.sdp)+"-"+os.path.basename(self.ddp)+".json")
logging.info("displaying first image grid")
self.walk(self.sdp)
listmax = min(gui.squaresperpage.get(), len(self.imagelist))
sublist = self.imagelist[0:listmax]
self.generatethumbnails(sublist)
gui.displaygrid(self.imagelist, range(0, min(len(self.imagelist), gui.squaresperpage.get())))
elif gui.sdpEntry.get() == gui.ddpEntry.get():
gui.sdpEntry.delete(0, len(gui.sdpEntry.get()))
gui.ddpEntry.delete(0, len(gui.ddpEntry.get()))
gui.sdpEntry.insert(0, "PATHS CANNOT BE SAME")
gui.ddpEntry.insert(0, "PATHS CANNOT BE SAME")
else:
gui.sdpEntry.delete(0, len(gui.sdpEntry.get()))
gui.ddpEntry.delete(0, len(gui.ddpEntry.get()))
gui.sdpEntry.insert(0, "ERROR INVALID PATH")
gui.ddpEntry.insert(0, "ERROR INVALID PATH")
def setup(self, dest):
# scan the destination
self.destinations = []
self.destinationsraw = []
with os.scandir(dest) as it:
for entry in it:
if entry.is_dir():
random.seed(entry.name)
self.destinations.append(
{'name': entry.name, 'path': entry.path, 'color': randomColor()})
self.destinationsraw.append(entry.path)
def makethumb(self, imagefile):
im = pyvips.Image.new_from_file(imagefile.path,)
hash = md5()
hash.update(im.write_to_memory())
imagefile.setid(hash.hexdigest())
thumbpath = os.path.join("data", imagefile.id+os.extsep+"jpg")
if os.path.exists(thumbpath):
imagefile.thumbnail = thumbpath
else:
try:
im = pyvips.Image.thumbnail(imagefile.path, self.thumbnailsize)
im.write_to_file(thumbpath)
imagefile.thumbnail = thumbpath
except Exception as e:
logging.error("Error:: %s", e)
def generatethumbnails(self, images):
logging.info("md5 hashing %s files", len(images))
with concurrent.ThreadPoolExecutor(max_workers=self.threads) as executor:
executor.map(self.makethumb, images)
logging.info("Finished making thumbnails")
def clear(self, *args):
if askokcancel("Confirm", "Really clear your selection?"):
for x in self.imagelist:
x.checked.set(False)
# Run Program
if __name__ == '__main__':
format = "%(asctime)s: %(message)s"
logging.basicConfig(
format=format, level=logging.WARNING, datefmt="%H:%M:%S")
mainclass = SortImages()