-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecycle.py
186 lines (156 loc) · 6.47 KB
/
recycle.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
import gc
import os
import pathlib
from datetime import datetime
from pathlib import Path
from tkinter import Canvas, Frame, Label, PhotoImage, Toplevel
from tkinter.constants import LEFT
from PIL import Image, ImageTk
from snapshot import Snapshot
folder = configDir = os.path.join(os.getenv("appdata"), "screenCap")
kbCons = 1024
class RecycleBin:
def __init__(self, size, mainWindow):
self.size = size
self.mainWindow = mainWindow
self.main = mainWindow.main
self.windowOpen = False
self.window : Toplevel = None
self.frameSize = (
int(self.main.winfo_screenwidth() / 6),
int(self.main.winfo_screenheight() / 5),
)
self.filePaths = []
self.thumbnails = []
self.hashes = set()
self.loadThumbs(loadImage=False)
# returns PhotoImage
def loadThumbs(self, loadImage=True):
paths = sorted(pathlib.Path(folder).glob("*.png"), key=lambda x: -os.path.getmtime(x))
# remove extra images
if len(paths) > self.size:
for i in range(len(paths) - 1, self.size - 1, -1):
os.remove(paths[i])
paths.pop(i)
self.filePaths = paths
self.hashes = {p.name for p in paths}
self.thumbnails.clear()
if loadImage:
for path in self.filePaths:
image = Image.open(path)
scale = min(self.frameSize[0] / image.width, self.frameSize[1] / image.height)
pimage = ImageTk.PhotoImage(image.resize([int(image.width * scale), int(image.height * scale)]))
self.thumbnails.append(pimage)
image.close()
else:
self.thumbnails = [None for _ in range(len(self.filePaths))]
# expecting pillow image
def addImage(self, image: Image.Image, name=None):
index = 0
oldPath = None
for i, p in enumerate(self.filePaths):
if p.name == name:
index = i
oldPath = p
break
if oldPath is not None and name != None and name in self.hashes:
self.hashes.remove(name)
oldPath = oldPath.rename(oldPath.with_name(datetime.now().strftime("%d%m%y_%H-%M-%S-%f") + ".png"))
self.filePaths.pop(index)
self.thumbnails.pop(index)
scale = min(self.frameSize[0] / image.width, self.frameSize[1] / image.height)
self.filePaths.insert(0, oldPath)
self.thumbnails.insert(
0, ImageTk.PhotoImage(image.resize([int(image.width * scale), int(image.height * scale)]))
)
self.hashes.add(oldPath.name)
else:
path = os.path.join(folder, datetime.now().strftime("%d%m%y_%H-%M-%S-%f") + ".png")
image.save(path, "PNG", compress_level=1)
self.filePaths.insert(0, Path(path))
self.hashes.add(os.path.basename(path))
scale = min(self.frameSize[0] / image.width, self.frameSize[1] / image.height)
pImage = ImageTk.PhotoImage(image.resize([int(image.width * scale), int(image.height * scale)]))
self.thumbnails.insert(0, pImage)
if len(self.filePaths) > self.size:
self.thumbnails.pop(-1)
os.remove(self.filePaths[-1])
self.hashes.remove(self.filePaths[-1].name)
self.filePaths.pop(-1)
if self.windowOpen:
for f in self.mainFrame.winfo_children():
f.destroy()
del f
self.mainFrame.update()
self.populate()
def show(self):
if self.windowOpen and self.window is not None:
self.window.deiconify()
self.window.lift()
return
self.loadThumbs(loadImage=True)
self.windowOpen = True
self.window = Toplevel(self.mainWindow.main)
self.window.title("Recycling Bin")
def exit():
self.windowOpen = False
self.window.destroy()
self.window = None
self.filePaths.clear()
self.thumbnails.clear()
self.visible = False
gc.collect()
self.window.protocol("WM_DELETE_WINDOW", lambda: exit())
self.canvas = Canvas(
self.window,
width=self.frameSize[0],
height=(4 * self.frameSize[1]),
borderwidth=0,
)
self.mainFrame = Frame(self.canvas)
self.window.bind(
"<MouseWheel>",
lambda event: self.canvas.yview_scroll(-1 * (event.delta // 120), "units"),
)
# self.window.resizable(False, False)
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas.create_window((0, 0), window=self.mainFrame, anchor="nw")
self.mainFrame.bind(
"<Configure>",
lambda event: self.canvas.configure(scrollregion=self.canvas.bbox("all")),
)
self.populate()
def populate(self):
maxWidth = 0
for i in range(len(self.filePaths)):
f = ImageFrame(self.thumbnails[i], self.mainWindow, self.filePaths[i], self.mainFrame)
f.pack(side="top")
temp = f.img.winfo_reqwidth() + f.text.winfo_reqwidth() + 20
if temp > maxWidth:
maxWidth = temp
self.mainFrame.update()
self.canvas.configure(width=maxWidth)
self.canvas.update()
self.window.focus_force()
class ImageFrame(Frame):
def __init__(self, image: PhotoImage, mainWindow, filePath: Path, *args, **kwargs):
self.image = image
self.filePath = filePath
self.mainWindow = mainWindow
self.pilImage = Image.open(self.filePath)
super(ImageFrame, self).__init__(*args, **kwargs)
self.img = Label(self, image=self.image)
self.img.pack(side=LEFT)
self.text = Label(
self,
text=f"""{datetime.fromtimestamp(os.path.getmtime(self.filePath)).strftime('%b.%d.%Y %I:%M|%p')}
{self.pilImage.width}x{self.pilImage.height}
{'%.2f' % (os.path.getsize(self.filePath)/kbCons)}kb""",
)
self.text.pack(side=LEFT, padx=10)
self.pilImage.close()
def show(path: Path):
Snapshot(mainWindow=self.mainWindow).fromImage(Image.open(path), path.name)
self.img.bind("<Button-1>", lambda event: show(self.filePath))
self.text.bind("<Button-1>", lambda event: show(self.filePath))
self.bind("<Button-1>", lambda event: show(self.filePath))