-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflower_sort.py
95 lines (80 loc) · 3.4 KB
/
flower_sort.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
import cv2
import os
import shutil
import concurrent.futures
from pathlib import Path
import tkinter as tk
from tkinter import filedialog
def is_image_clear(image_path, threshold=100):
image = cv2.imread(str(image_path))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
laplacian = cv2.Laplacian(gray, cv2.CV_64F).var()
return laplacian > threshold
def find_single_flower(image_path):
image = cv2.imread(str(image_path))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 1:
return True
else:
return False
def select_clear_images(folder_path, num_images):
clear_images = []
image_paths = [file for file in Path(folder_path).glob('*.jpg') if file.is_file() or file.suffix in ['.jpeg', '.png', '.bmp']]
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = {executor.submit(is_image_clear, path): path for path in image_paths}
for future in concurrent.futures.as_completed(futures):
path = futures[future]
if future.result():
if find_single_flower(path):
clear_images.append(path)
if len(clear_images) == num_images:
break
return clear_images
def rename_files(folder_path):
files = sorted(Path(folder_path).iterdir(), key=os.path.getmtime)
for i, file in enumerate(files):
if file.is_file():
new_filename = f"{str(i+1).zfill(2)}{file.suffix}"
file.rename(file.with_name(new_filename))
def main():
print("Select an operation:")
print("1. Select clear images with single flowers")
print("2. Rename all files")
operation = input("Enter your choice (1 or 2): ")
root = tk.Tk()
root.withdraw()
folder_path = filedialog.askdirectory(title="Select Folder Containing Images")
if not os.path.exists(folder_path):
print("Invalid folder path.")
return
if operation == "1":
num_images = int(input("Enter the number of clear images with single flowers you want to select: "))
clear_images = select_clear_images(folder_path, num_images)
# Prompt the user for saving options
print("Select where to save the clear images:")
print("1. Save in a new folder in the same location")
print("2. Select a different output folder")
save_option = input("Enter your choice (1 or 2): ")
if save_option == "1":
output_folder = os.path.join(folder_path, "clear_images")
if not os.path.exists(output_folder):
os.makedirs(output_folder)
elif save_option == "2":
output_folder = filedialog.askdirectory(title="Select Output Folder to Save Clear Images")
if not os.path.exists(output_folder):
os.makedirs(output_folder)
else:
print("Invalid choice.")
return
for image_path in clear_images:
shutil.copy(str(image_path), output_folder)
print(f"{len(clear_images)} clear images with single flowers saved in '{output_folder}'.")
elif operation == "2":
rename_files(folder_path)
print("Files renamed successfully.")
else:
print("Invalid choice.")
if __name__ == "__main__":
main()