Hello, I have asked chatGPT to create python program where I select image and I want to zoom into an selected area, then click a pixel and then to get the color RGB/HSV of it. It created everything well except one problem. The new created window Figure 2 does not copy the image from Figure 1 (the original Zoom window). It seems that it is not possible to select a color from fig, in Figure 1 so it create Figure 2. But as said, Figure 2 does not contain the image from Figure 2. And also if Figure 2 is necessery to be there, then it should cover the Figure 1 and be on a top to make sure user cannot switch to Figure 1 by incident and confuse the windows. Can anyone please to fix it?
Edit
I have updated the code. Do you know how to make the program wait till the Zoom window (Figure 2) is open? When I click on Zoom tool, then I select area, I need to wait till the new window is open and the zoom image is shown there. Then I can run the method do_job().
import cv2 # pip install opencv-python-headless
import matplotlib.pyplot as plt # pip install matplotlib
from matplotlib.patches import Rectangle
from tkinter import Tk, Canvas, PhotoImage, filedialog
import configparser
from ttkthemes import ThemedTk, ThemedStyle
class ImageAnalyzer:
def __init__(self):
self.click_1 = (0, 0)
self.click_2 = (0, 0)
self.selected_area = (0, 0, 0, 0)
self.large_image = None
self.fig = None
self.zoom_on = False
def load_image(self):
try:
filename = self.load_filename()
except KeyError:
root = ThemedTk(theme="black")
filename = filedialog.askopenfilename()
self.save_filename(filename)
root.destroy()
self.large_image = cv2.imread(filename)
self.large_image = cv2.cvtColor(self.large_image, cv2.COLOR_BGR2RGB)
self.fig, ax = plt.subplots()
tools = self.fig.canvas.toolbar.children
# přiřazení k tlačítku zoom závisí na identifikátoru - Zoom je checkbutton
tools['!checkbutton2'].bind('<Button-1>', self.zoom_switch)
ax.invert_yaxis()
ax.imshow(self.large_image)
ax.set_title('Select an area to zoom in')
self.cid_pressed = self.fig.canvas.mpl_connect('button_press_event', self.pressed)
self.cid_released = self.fig.canvas.mpl_connect('button_release_event', self.released)
plt.show()
def save_filename(self, filename):
config = configparser.ConfigParser()
config['DEFAULT'] = {'filename': filename}
with open('config.cfg', 'w') as configfile:
config.write(configfile)
def load_filename(self):
config = configparser.ConfigParser()
config.read('config.cfg')
return config['DEFAULT']['filename']
def zoom_switch(self, event):
if self.zoom_on == False:
self.zoom_on = True
else:
self.zoom_on = False
def pressed(self, event):
if not self.zoom_on:
self.click_1 = (0, 0)
self.click_2 = (0, 0)
return
self.click_1 = (int(event.xdata), int(event.ydata))
self.fig.canvas.mpl_disconnect(self.cid_pressed)
def released(self, event):
if not self.zoom_on:
return
self.click_2 = (int(event.xdata), int(event.ydata))
self.fig.canvas.mpl_disconnect(self.cid_released)
def do_job(self, event):
fig, ax = plt.subplots()
ax.imshow(self.large_image[self.selected_area[1]:, self.selected_area[0]:])
ax.set_title('Click to select pixel')
self.cid = fig.canvas.mpl_connect('button_press_event', self.pick_color)
plt.show()
def pick_color(self, event):
x, y = int(event.xdata), int(event.ydata)
pixel_color = cv2.cvtColor(self.large_image[y:y+1, x:x+1], cv2.COLOR_BGR2RGB)[0,0]
print("RGB:", pixel_color)
hsl_color = cv2.cvtColor(self.large_image[y:y+1, x:x+1], cv2.COLOR_BGR2HLS)[0,0]
print("HSL:", hsl_color)
def run(self):
self.load_image()
'''
root = Tk()
style = ThemedStyle(root)
installed_themes = style.theme_names()
print(installed_themes)
'''
if __name__ == "__main__":
analyzer = ImageAnalyzer()
analyzer.run()
I notice this part of code:
def do_job(self, event):
fig, ax = plt.subplots()
The fig... needs to be assigned later when the Figure 2 is finished. Then I need to access the fig[1] if possible because I expect that I need to access the second window somehow!