added image utils

This commit is contained in:
Alexander Karpov 2023-03-08 13:41:14 +03:00
parent b81681b9d7
commit 90ea3f5d5f
3 changed files with 231 additions and 0 deletions

79
images/classifier.py Normal file
View File

@ -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]))

29
images/clear.py Normal file
View File

@ -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")

123
images/manage.py Normal file
View File

@ -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("<Return>", 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()