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
|
#!/usr/bin/env python3
import os
import subprocess
def sizeinbytes(size, unit):
mag = {'KiB' : 1, 'MiB': 2, 'GiB' : 3}
return float(size) * (1000 ** mag[unit])
# Run czkawka_cli on the current directory
cwd = os.getcwd()
print("Running czkawa for directory ", cwd, "...", sep="")
czkawka = ["czkawka_cli", "image", "-d", cwd]
czkawkadata = subprocess.run(czkawka, stdout=subprocess.PIPE)
czkawkadata = czkawkadata.stdout.decode('utf-8').splitlines() # Convert to a normal string and split into lines
if not czkawkadata:
print("All images in", cwd, "are unique. Exiting...")
exit()
# Get number of images total and remove first line since it would cause issues
numimages = int(czkawkadata[0].split()[1])
print ("Done!", numimages, "duplicate images found...")
czkawkadata.pop(0)
# Split czkawkadata by the delimeter used by czkawka_cli
czkawkadata = [i.split(' - ') for i in czkawkadata]
# Make keep store the images we want to keep
largest = 0
i = 0
dups = 0
keep = [None] * numimages
for image in czkawkadata:
if not image[0]:
largest = 0
i += 1
continue
dups += 1
size = image[2].split()
size = sizeinbytes(size[0], size[1]) # Convert to bytes so we can easily compate kb vs mb vs gb
if size > largest:
largest = size
keep[i] = image
# Remove all empty entries from czkawkadata
czkawkadata[:] = [x for x in czkawkadata if x[0]]
# Remove any images we're keeping from czkawkadata to be left with only the duplicates
for image in keep:
czkawkadata.remove(image)
# User actions
confirmation = None
message = "Are you sure you want to delete all " + str(dups - len(keep)) + " duplicate images? (Yes/No/Show) "
while not confirmation:
confirmation = input(message)
if (confirmation == "yes"):
for image in czkawkadata:
os.remove(image[0])
print("Removed image", image[0])
elif (confirmation == "no"):
exit()
elif (confirmation == "show"):
for image in czkawkadata:
print(image)
confirmation = None # Prompt again
else:
confirmation = None
|