From 90ea3f5d5f13382d577be906310b1a04902c6001 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Wed, 8 Mar 2023 13:41:14 +0300 Subject: [PATCH] added image utils --- images/classifier.py | 79 +++++++++++++++++++++++++++ images/clear.py | 29 ++++++++++ images/manage.py | 123 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 231 insertions(+) create mode 100644 images/classifier.py create mode 100644 images/clear.py create mode 100644 images/manage.py diff --git a/images/classifier.py b/images/classifier.py new file mode 100644 index 0000000..f199dac --- /dev/null +++ b/images/classifier.py @@ -0,0 +1,79 @@ +import os +import shutil +import numpy as np + +from PIL import Image +from pathlib import Path +from tkinter.filedialog import askdirectory +from tkinter import Tk + + +def test_color_distribution(file): + img = Image.open(file) + img.thumbnail((200, 200), Image.ANTIALIAS) + w, h = img.size + return sum( + x[0] + for x in sorted( + img.convert("RGB").getcolors(w * h), key=lambda x: x[0], reverse=True + )[:10] + ) / float((w * h)) + + +def test_sharp_edge_detection(file): + img = Image.open(file).convert("L") + values = abs(np.fft.fft2(np.asarray(img.convert("L")))).flatten().tolist() + high_values = [x for x in values if x > 10000] + high_values_ratio = 100 * (float(len(high_values)) / len(values)) + return high_values_ratio + + +def test_file(file): + image = 0 + real = 0 + color = test_color_distribution(file) + edges = test_sharp_edge_detection(file) + + if color > 0.28: + image += (color - 0.28) * 10 + else: + real += (0.28 - color) * 10 + + if edges > 13: + image += edges - 13 + else: + real += 13 - edges + return image > real + + +Tk().withdraw() + +# Dialog box for selecting a folder. +file_path = askdirectory(title="Select a folder") + +list_of_files = os.walk(file_path) + + +if not os.path.exists(os.path.join(file_path, "sorted")): + os.makedirs(os.path.join(file_path, "sorted")) + +if not os.path.exists(os.path.join(file_path, "sorted/image_real")): + os.makedirs(os.path.join(file_path, 'sorted/image_real')) + +real = os.path.join(file_path, 'sorted/image_real') + +if not os.path.exists(os.path.join(file_path, "sorted/drawn")): + os.makedirs(os.path.join(file_path, 'sorted/drawn')) + +drawn = os.path.join(file_path, 'sorted/drawn') + + +for root, folders, files in list_of_files: + for file in files: + file_path = Path(os.path.join(root, file)) + is_drawn = test_file(file_path) + if is_drawn: + shutil.move(file_path, os.path.join(drawn, str(file_path).split("/")[-1])) + else: + shutil.move(file_path, os.path.join(real, str(file_path).split("/")[-1])) + diff --git a/images/clear.py b/images/clear.py new file mode 100644 index 0000000..e149b34 --- /dev/null +++ b/images/clear.py @@ -0,0 +1,29 @@ +from tkinter.filedialog import askdirectory +from tkinter import Tk +import os +import hashlib +from pathlib import Path + +Tk().withdraw() + +# Dialog box for selecting a folder. +file_path = askdirectory(title="Select a folder") + +# Listing out all the files +# inside our root folder. +list_of_files = os.walk(file_path) + +# In order to detect the duplicate +# files we are going to define an empty dictionary. +unique_files = dict() + +for root, folders, files in list_of_files: + for file in files: + file_path = Path(os.path.join(root, file)) + hash_file = hashlib.md5(open(file_path, 'rb').read()).hexdigest() + + if hash_file not in unique_files: + unique_files[hash_file] = file_path + else: + os.remove(file_path) + print(f"{file_path} has been deleted") diff --git a/images/manage.py b/images/manage.py new file mode 100644 index 0000000..b96f0d2 --- /dev/null +++ b/images/manage.py @@ -0,0 +1,123 @@ +import os +import shutil +import tkinter + +from PIL import ImageTk, Image + +# Dialog box for selecting a folder. +file_path = input("Path: ") +files = [] +print("started loading files") +for dirpath, _, filenames in os.walk(file_path): + for f in filenames: + files.append(os.path.abspath(os.path.join(dirpath, f))) +print("files loaded") + + +if not os.path.exists(os.path.join(file_path, "sorted")): + os.makedirs(os.path.join(file_path, "sorted")) + +if not os.path.exists(os.path.join(file_path, "sorted/drawn")): + os.makedirs(os.path.join(file_path, "sorted/drawn")) + +drawn = os.path.join(file_path, "sorted/drawn") + +if not os.path.exists(os.path.join(file_path, "sorted/real")): + os.makedirs(os.path.join(file_path, "sorted/real")) + +real = os.path.join(file_path, "sorted/real") + +if not os.path.exists(os.path.join(file_path, "sorted/comic")): + os.makedirs(os.path.join(file_path, "sorted/comic")) + +comic = os.path.join(file_path, "sorted/comic") + +if not os.path.exists(os.path.join(file_path, "sorted/other")): + os.makedirs(os.path.join(file_path, "sorted/other")) + +other = os.path.join(file_path, "sorted/other") + +if not os.path.exists(os.path.join(file_path, "sorted/delete")): + os.makedirs(os.path.join(file_path, "sorted/delete")) + +delete = os.path.join(file_path, "sorted/delete") + + +class Window: + def __init__(self, master, files): + self.img = None + self.label = None + + self.files = files + self.cur = 0 + self.master = master + + init_file = self.files[0] + self.show_file(init_file) + + def show_file(self, f): + self.img = Image.open(f) + self.img = self.img.resize((self.img.width, self.img.height), Image.ANTIALIAS) + + self.img = ImageTk.PhotoImage(self.img) + + self.label = tkinter.Label(self.master, image=self.img) + self.label.pack(expand=True, fill=tkinter.BOTH) + + def next(self, event): + self.cur += 1 + self.show_file(self.files[self.cur]) + + def real(self, event): + self.label.destroy() + shutil.move( + self.files[self.cur], + os.path.join(real, str(self.files[self.cur]).split("/")[-1]), + ) + self.next(event) + + def drawn(self, event): + self.label.destroy() + shutil.move( + self.files[self.cur], + os.path.join(drawn, str(self.files[self.cur]).split("/")[-1]), + ) + self.next(event) + + def comic(self, event): + self.label.destroy() + shutil.move( + self.files[self.cur], + os.path.join(comic, str(self.files[self.cur]).split("/")[-1]), + ) + self.next(event) + + def other(self, event): + self.label.destroy() + shutil.move( + self.files[self.cur], + os.path.join(other, str(self.files[self.cur]).split("/")[-1]), + ) + self.next(event) + + def delete(self, event): + self.label.destroy() + shutil.move( + self.files[self.cur], + os.path.join(delete, str(self.files[self.cur]).split("/")[-1]), + ) + self.next(event) + + +root = tkinter.Tk() +root.title("1 - real; 2 - drawn; 3 - comic; 4 - other; 5 - delete") +window = Window(root, files) +root.bind("", window.next) +root.bind("1", window.real) +root.bind("2", window.drawn) +root.bind("3", window.comic) +root.bind("4", window.other) +root.bind("5", window.delete) +root.mainloop() + +root.mainloop()