import tkinter as tk
from PIL import Image, ImageTk
classImageApp:def__init__(self, root, image_path,filename="result.csv"):
self.root = root
self.root.title("Image Viewer")
self.file=open(filename,'w', encoding='utf-8')# Load the image
self.image = Image.open(image_path)
self.tk_image = ImageTk.PhotoImage(self.image)# Create canvas for displaying image
self.canvas = tk.Canvas(root, width=self.image.width, height=self.image.height)
self.canvas.pack()# Display the image on the canvas
self.canvas.create_image(0,0, anchor=tk.NW, image=self.tk_image)# Bind mouse click event to canvas
self.canvas.bind("<Button-1>", self.on_canvas_click)# Initialize variables
self.coordinate_groups =[]
self.group_counter =1# Create a button to save coordinates
self.save_button = tk.Button(root, text="Save Coordinates", command=self.save_coordinates)
self.save_button.pack()defon_canvas_click(self, event):# Output the clicked coordinatesprint(f"Clicked at: ({event.x}, {event.y})")# Store coordinates
self.coordinate_groups.append((event.x, event.y))defsave_coordinates(self):ifnot self.coordinate_groups:print("No coordinates to save.")return# Save all coordinates to a CSV file with a sequential number
self.file.write(str(self.group_counter)+","+str(self.coordinate_groups)+"\n")
self.coordinate_groups.clear()
self.group_counter = self.group_counter +1if __name__ =="__main__":# Replace 'path/to/your/image.jpg' with the actual path to your image file
image_path =r'D:\traffic_model\天津.png'# Create the main window
root = tk.Tk()
save_file="天津.csv"# Create an instance of the ImageApp class
app = ImageApp(root, image_path,save_file)# Start the main loop
root.mainloop()