From d75d53fe79ee7cf0d2efd30d5e191def8c746056 Mon Sep 17 00:00:00 2001
From: Timmy Keller <tjk@tjkeller.xyz>
Date: Tue, 26 Oct 2021 02:25:17 -0500
Subject: changed a bunch of video scripts and added my first and possibly last
 python script, although it was fun

---
 video/rmduppic | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100755 video/rmduppic

(limited to 'video/rmduppic')

diff --git a/video/rmduppic b/video/rmduppic
new file mode 100755
index 0000000..6fdc18d
--- /dev/null
+++ b/video/rmduppic
@@ -0,0 +1,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
-- 
cgit v1.2.3