Project

General

Profile

Feature #1122 ยป ver2.py

Sushaant Kathirvel Murugan, 10/27/2024 06:06 PM

 
1
import cv2 as cv
2
import numpy as np
3
import tkinter as tk
4
from tkinter import filedialog, messagebox
5
import os
6
from PIL import Image, ImageTk
7
class SharedData:
8
    def __init__(self):
9
        self.left_image = None
10
        self.right_image = None
11
        self.overlap_region = 0
12
class ImageIO:
13
    def __init__(self, shared_data):
14
        self.shared_data = shared_data
15
    def load_image(self, path):
16
        return cv.imread(path, cv.IMREAD_COLOR)
17
    def write_image(self, img, fileName="image.png"):
18
        if cv.imwrite(fileName, img):
19
            print(f"File {fileName} Written Successfully")
20
        else:
21
            print("File writing failed")
22
    def show_image(self, img, title="Image Title", canvas=None):
23
        img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
24
        img_pil = Image.fromarray(img_rgb)
25
        img_tk = ImageTk.PhotoImage(img_pil)
26
        canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
27
        canvas.image = img_tk
28
class Commands:
29
    def __init__(self, gui, shared_data):
30
        self.gui = gui
31
        self.shared_data = shared_data
32
        self.io = ImageIO(shared_data)
33
    def load_left_image(self):
34
        filepath = filedialog.askopenfilename(title="Select Left Image")
35
        if filepath:
36
            self.shared_data.left_image = self.io.load_image(filepath)
37
            print("Left image loaded.")
38
    def load_right_image(self):
39
        filepath = filedialog.askopenfilename(title="Select Right Image")
40
        if filepath:
41
            self.shared_data.right_image = self.io.load_image(filepath)
42
            print("Right image loaded.")
43
    def show_left_image(self):
44
        if self.shared_data.left_image is not None:
45
            self.io.show_image(self.shared_data.left_image, canvas=self.gui.canvas)
46
            print("Left image displayed.")
47
        else:
48
            messagebox.showerror("Error", "No left image loaded!")
49
    def show_right_image(self):
50
        if self.shared_data.right_image is not None:
51
            self.io.show_image(self.shared_data.right_image, canvas=self.gui.canvas)
52
            print("Right image displayed.")
53
        else:
54
            messagebox.showerror("Error", "No right image loaded!")
55
    def set_overlap_region(self):
56
        self.shared_data.overlap_region = self.gui.overlap.get()
57
        print(f"Overlap region set to {self.shared_data.overlap_region}")
58

    
59
 # \class GUI
60
 # \brief This class creates a graphical user interface for the image projector.
61
 #
62
 # The GUI class is responsible for initializing the main window and setting up the control window. It provides
63
 # a canvas for displaying images and buttons for loading and displaying images. 
64
 #
65
 # \author
66
 # \date 2024-10-27
67
class GUI:
68
    # \brief Constructor for the GUI class.
69
    #
70
    # This constructor initializes the main window and sets up the control window.
71
    #
72
    # \param master the parent window
73
    def __init__(self, master):
74

    
75
        self.master = master
76
        # \brief Set the title of the main window
77
        self.master.title("Image Projector")
78
        # \brief Create a canvas for displaying images
79
        #
80
        # The canvas is used to project images loaded through the control window. It is set to black.
81
        self.canvas = tk.Canvas(self.master, bg="black")
82
        # \brief Pack the canvas to fill the window.
83
        #
84
        # The canvas is packed to fill the window. The "fill = tk.BOTH" parameter stretches the canvas both horizontally and vertically.
85
        self.canvas.pack(fill=tk.BOTH, expand=True)
86
        # \brief Initialize the shared data class.
87
        #
88
        # Holds information about the images and overlap region.
89
        self.shared_data = SharedData()
90
        # \brief Initialize the commands class.
91
        #
92
        # Handles the commands for loading and displaying images.
93
        # \param self the object pointer
94
        # \param self.shared_data the shared data class
95
        self.commands = Commands(self, self.shared_data)
96
        self.control_window()
97
    # \brief Call the function to set up the control window.
98
    #
99
    # A seperate window which allows loading and setting up images and overlap region. It contains buttons and entry
100
    # fields for loading images, setting the overlap region and control the display of images.
101
    def control_window(self):
102
        # \brief Initalize the control window.
103
        self.control_window = tk.Toplevel(self.master)
104
        # \brief Set the title of the control window.
105
        self.control_window.title("Controls")
106
        # \brief Initialize the overlap variable to store the overlap region width.
107
        #
108
        # The overlap variable will determine the region of overlap between the left and right images.
109
        self.overlap = tk.IntVar()
110
        # \brief Create a label for overlap region input box.
111
        self.label = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
112
        # \brief Create an entry box for user to input the overlap region width.
113
        #
114
        # The entry box will take the user input for the overlap region width.
115
        self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
116
        # \brief Pack the label and entry box into the control window.
117
        self.label.pack()
118
        self.overlapBox.pack()
119
        # \brief Create a button to set the overlap region based on user input.
120
        #
121
        # This button will call the set_overlap_region function from the commands class.
122
        self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
123
        self.set_overlap_button.pack()
124
        # \brief Create a button to load the left image.
125
        #
126
        # This button will call the load_left_image function from the commands class.
127
        self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
128
        self.button_load_left.pack()
129
        # \brief Create a button to load the right image.
130
        #
131
        # This button will call the load_right_image function from the commands class.
132
        self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
133
        self.button_load_right.pack()
134
        # \brief Create a button to display the left image.
135
        #
136
        # This button will call the show_left_image function from the commands class.
137
        self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
138
        self.button_show_left.pack()
139
        # \brief Create a button to display the right image.
140
        #
141
        # This button will call the show_right_image function from the commands class.
142
        self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
143
        self.button_show_right.pack()
144

    
145
# \brief Main program entry point.
146
#
147
# This block initializes the tkinter root window, sets the window size and initializes the GUI class.
148
if __name__ == "__main__":
149
# \brief Initialize the tkinter root window.
150
    root = tk.Tk()
151
    # \brief Set the window size to 1280x800.
152
    root.geometry("1280x800")
153
    # \brief Create an instance of the GUI class passing the root window.
154
    app = GUI(root)
155
    # \brief Start the tkinter main loop to display the window and handle events.
156
    root.mainloop()
    (1-1/1)