Project

General

Profile

Files » ver3.py

Working version - Md Tamjidur RAHMAN, 10/24/2024 03:14 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

    
8
class SharedData:
9
    def __init__(self):
10
        self.left_image = None
11
        self.right_image = None
12
        self.overlap_region = 200
13
        self.alpha = 0.5
14

    
15
        self.leftSide = True
16

    
17
        # Methods to implement
18
        # Load config function
19
        # Write to config file 
20

    
21
class ImageIO:
22
    def __init__(self, shared_data):
23
        self.shared_data = shared_data
24

    
25
    def load_image(self, path):
26
        return cv.imread(path, cv.IMREAD_COLOR)
27

    
28
    def write_image(self, img, fileName="image.png"):
29
        if cv.imwrite(fileName, img):
30
            print(f"File {fileName} Written Successfully")
31
        else:
32
            print("File writing failed")
33

    
34
    def show_image(self, img, title="Image Title", canvas=None):
35
        img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
36
        img_pil = Image.fromarray(img_rgb)
37
        img_tk = ImageTk.PhotoImage(img_pil)
38
        
39
        canvas.update_idletasks()  # Force the canvas to update its dimensions
40
        canvas_width = canvas.winfo_width()  # Get the updated canvas width
41
        
42
        if self.shared_data.isLeft:
43
            # Align image to the top-right corner
44
            canvas.create_image(canvas_width, 0, anchor=tk.NE, image=img_tk)
45
        else:
46
            # Align image to the top-left corner
47
            canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
48
        
49
        canvas.image = img_tk  # Prevent image from being garbage collected
50

    
51

    
52

    
53
class Commands:
54
    def __init__(self, gui, shared_data):
55
        self.gui = gui
56
        self.shared_data = shared_data
57
        self.io = ImageIO(shared_data)
58

    
59
    def load_left_image(self):
60
        filepath = filedialog.askopenfilename(title="Select Left Image")
61
        if filepath:
62
            self.shared_data.left_image = self.io.load_image(filepath)
63
            print("Left image loaded.")
64

    
65
    def load_right_image(self):
66
        filepath = filedialog.askopenfilename(title="Select Right Image")
67
        if filepath:
68
            self.shared_data.right_image = self.io.load_image(filepath)
69
            print("Right image loaded.")
70

    
71
    def show_left_image(self):
72
        if self.shared_data.left_image is not None:
73
            self.shared_data.isLeft = True
74
            # Create a copy of the original left image
75
            left = self.shared_data.left_image.copy()
76

    
77
            # Assuming both images have the same height
78
            height = left.shape[0]
79

    
80
            left_overlap_start = left.shape[1]-self.shared_data.overlap_region
81

    
82
            # Extract the overlapping region
83
            left_overlap = left[0:height, left_overlap_start:, :]
84

    
85
            # Adjust the alpha value
86
            left_overlap_mod = cv.convertScaleAbs(left_overlap, alpha=self.shared_data.alpha, beta=0)  # Adjust brightness
87

    
88
            # Replace the modified blue channel back into the original right image
89
            left[0:height, left_overlap_start:, :] = left_overlap_mod
90

    
91
            # Show the modified image
92
            self.io.show_image(left, canvas=self.gui.canvas)
93
            print("Left image displayed.")
94
        else:
95
            messagebox.showerror("Error", "No left image loaded!")
96

    
97
    def show_right_image(self):
98
        if self.shared_data.right_image is not None:
99
            
100
            self.shared_data.isLeft = False
101
            # Create a copy of the original right image
102
            right = self.shared_data.right_image.copy()
103
        
104
            # Assuming both images have the same height
105
            height = right.shape[0]
106

    
107
            right_overlap_start = 0
108
            right_overlap_end = self.shared_data.overlap_region
109

    
110
            # Extract the overlapping region
111
            right_overlap = right[0:height, right_overlap_start:right_overlap_end, :]
112

    
113
            # Adjust the alpha value
114
            right_overlap_mod = cv.convertScaleAbs(right_overlap, alpha=self.shared_data.alpha, beta=0)  # Adjust brightness
115

    
116
            # Replace the modified blue channel back into the original right image
117
            right[0:height, right_overlap_start:right_overlap_end, :] = right_overlap_mod
118

    
119
            # Show the modified image
120
            self.io.show_image(right, canvas=self.gui.canvas)
121
            print("Right image displayed.")
122
        else:
123
            messagebox.showerror("Error", "No right image loaded!")
124

    
125

    
126
    def set_overlap_region(self):
127
        self.shared_data.overlap_region = self.gui.overlap.get()
128
        print(f"Overlap region set to {self.shared_data.overlap_region}")
129

    
130
    def set_alpha_value(self):
131
        alpha = self.gui.alpha.get()
132
        if(alpha > 1.0):
133
            alpha = 1
134
            
135
        if(alpha < 0.0):
136
            alpha = 0
137

    
138
        self.shared_data.alpha = alpha
139
        print(f"Alpha value set to {self.shared_data.alpha}")
140

    
141

    
142

    
143
class GUI:
144
    def __init__(self, master):
145
        self.master = master
146
        self.master.title("Image Projector")
147
        self.canvas = tk.Canvas(self.master, bg="black")
148
        self.canvas.pack(fill=tk.BOTH, expand=True)
149
        self.shared_data = SharedData()
150
        self.commands = Commands(self, self.shared_data)
151
        self.control_window()
152

    
153
    def control_window(self):
154
        self.control_window = tk.Toplevel(self.master)
155
        self.control_window.title("Controls")
156
        
157
        self.overlap = tk.IntVar(value=200)
158
        self.alpha = tk.DoubleVar(value=0.5)
159

    
160
        self.alphaLabel = tk.Label(self.control_window, text='Alpha value', font=('calibre', 10, 'bold'))
161
        self.alphaBox = tk.Entry(self.control_window, textvariable=self.alpha)
162
        self.alphaLabel.pack()
163
        self.alphaBox.pack()
164

    
165
        self.set_alpha_button = tk.Button(self.control_window, text="Set Alpha", command=self.commands.set_alpha_value)
166
        self.set_alpha_button.pack()
167

    
168
        self.overlapLabel = tk.Label(self.control_window, text='Overlap region Width', font=('calibre', 10, 'bold'))
169
        self.overlapBox = tk.Entry(self.control_window, textvariable=self.overlap)
170
        self.overlapLabel.pack()
171
        self.overlapBox.pack()
172

    
173

    
174
        self.set_overlap_button = tk.Button(self.control_window, text="Set Overlap Region", command=self.commands.set_overlap_region)
175
        self.set_overlap_button.pack()
176

    
177
        self.button_load_left = tk.Button(self.control_window, text="Load Left Image", command=self.commands.load_left_image)
178
        self.button_load_left.pack()
179

    
180
        self.button_load_right = tk.Button(self.control_window, text="Load Right Image", command=self.commands.load_right_image)
181
        self.button_load_right.pack()
182

    
183
        self.button_show_left = tk.Button(self.control_window, text="Show Left Image", command=self.commands.show_left_image)
184
        self.button_show_left.pack()
185

    
186
        self.button_show_right = tk.Button(self.control_window, text="Show Right Image", command=self.commands.show_right_image)
187
        self.button_show_right.pack()
188

    
189
        
190

    
191
if __name__ == "__main__":
192
    root = tk.Tk()
193
    root.geometry("1280x800")
194
    app = GUI(root)
195
    root.mainloop()
(11-11/49)