-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (86 loc) · 4.49 KB
/
main.py
File metadata and controls
107 lines (86 loc) · 4.49 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk
import numpy as np
import cv2
from quick_sort import quick_sort, scale_to_fit
import random
from merge_sort import merge_sort
#sort image array using the selected algorithm
#def sort_image(image_array, algorithm):
# flat_array = image_array.flatten()
# if algorithm == "MergeSort":
# merge_sort(flat_array)
# elif algorithm == "QuickSort":
# flat_array = quick_sort(list(flat_array))
#reshape array
# return flat_array.reshape(image_array.shape)
#main gui class
class ImageSorterApp:
def __init__(self, root):
self.root = root
self.root.title("Image Sorting Application")
self.image_path = None
self.original_image = None
self.image_array = None
#gui elements
self.label = tk.Label(self.root, text="Image Sorting Application", font=("Arial", 16))
self.label.pack(pady=10)
self.select_button = tk.Button(self.root, text="Select Image", command=self.load_image)
self.select_button.pack(pady=5)
#sorting buttons
self.mergesort_button = tk.Button(self.root, text="Use Merge Sort",
command=lambda: self.sort_and_display("MergeSort"))
self.mergesort_button.pack(pady=5)
self.quicksort_button = tk.Button(self.root, text="Use Quick Sort", command=lambda: self.sort_and_display("QuickSort"))
self.quicksort_button.pack(pady=5)
def load_image(self):
#open dialog to select an image file
file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp")])
if not file_path: # If the user cancels the dialog
return
self.image_path = file_path # Store the selected file path
self.original_image = cv2.imread(self.image_path) # Read the image using OpenCV
if self.original_image is None: # If OpenCV fails to read the image
messagebox.showerror("Error", "Unable to load image. Please select a valid image file.")
return
self.image_array = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2RGB) # Convert to RGB for PIL compatibility
messagebox.showinfo("Image Loaded", "Image has been successfully loaded!")
#this function isnt working rn
def sort_and_display(self, algorithm):
if self.image_array is None:
messagebox.showerror("Error", "No image loaded! Please select an image first.")
return
#put syeds code here for scrambling the image and then add sort_image function call. MAKE SURE TO DISPLAY UNSORTED IMAGE FIRST
#read the image and save it and then the .image function from the numpy library gets the height and width in pixels
image = cv2.imread(f"{self.image_path}")
height, width, _ = image.shape
#this creates a dictioanry where each key represents a column index
#each value is an arrray of arrays of the rgb col values
column_slices = {i: image[:, i] for i in range(width)}
#this takes the keys of the dictionary and puts them into a list
index_list = list(column_slices.keys())
#this randomizes the indices for each column index of the image
random.shuffle(index_list)
#this uses hstack to combine columns together from the dictionary of column slices
#it loops through each index in the random index list and then takes the column value from the corresponding
#key in the dictionary and then horizontally combines them into a 2d array to represent the shuffled image
shuffled_img = np.hstack([column_slices[index] for index in index_list])
shuffled_img_scaled = scale_to_fit(shuffled_img) # Scale the shuffled image
#this displays the shuffled image in a window called image and waits for a key to be pressed
cv2.imshow('image', shuffled_img_scaled)
cv2.waitKey(0)
#sorted_img = shuffled_img.copy()
if algorithm == "QuickSort":
quick_sort(index_list, 0, len(index_list) - 1, column_slices, 'image', 1)
else:
merge_sort(index_list, 0, len(index_list) - 1, column_slices, 'image', 1)
sorted_img = np.hstack([column_slices[index] for index in index_list])
sorted_img_scaled = scale_to_fit(sorted_img) # Scale the sorted image
cv2.imshow('image', sorted_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
root = tk.Tk()
app = ImageSorterApp(root)
root.mainloop()