Here's a Python program using OpenCV and Tkinter that meets your requirements:
import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog, simpledialog, StringVar
from PIL import Image, ImageTk
import math
class AngleMeasurementApp:
def __init__(self, root):
self.root = root
self.root.title("Angle Measurement Tool")
# Variables
self.img = None
self.original_img = None
self.display_img = None
self.first_line_type = None # 'horizontal' or 'vertical'
self.angle = 0
self.angle_var = StringVar(value="0.00°")
self.is_dragging = False
self.line_end_point = None
self.canvas_width = 800
self.canvas_height = 600
# Create UI
self.create_widgets()
def create_widgets(self):
# Top frame for buttons
top_frame = tk.Frame(self.root)
top_frame.pack(fill=tk.X, padx=10, pady=5)
# Load image button
load_btn = tk.Button(top_frame, text="Load Image", command=self.load_image)
load_btn.pack(side=tk.LEFT, padx=5)
# Angle display with copy button
angle_frame = tk.Frame(top_frame)
angle_frame.pack(side=tk.RIGHT, padx=5)
tk.Label(angle_frame, text="Angle:").pack(side=tk.LEFT)
angle_entry = tk.Entry(angle_frame, textvariable=self.angle_var, width=10,
readonlybackground='white')
angle_entry.pack(side=tk.LEFT, padx=5)
angle_entry.config(state='readonly')
copy_btn = tk.Button(angle_frame, text="Copy", command=self.copy_angle)
copy_btn.pack(side=tk.LEFT)
# Canvas for image display
self.canvas_frame = tk.Frame(self.root, bd=2, relief=tk.SUNKEN)
self.canvas_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
self.canvas = tk.Canvas(self.canvas_frame, width=self.canvas_width,
height=self.canvas_height, bg='gray')
self.canvas.pack(fill=tk.BOTH, expand=True)
# Instructions
instructions = "Click and drag to rotate the second line. The angle will be displayed above."
tk.Label(self.root, text=instructions, pady=5).pack()
# Bind mouse events
self.canvas.bind("<ButtonPress-1>", self.on_mouse_down)
self.canvas.bind("<B1-Motion>", self.on_mouse_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_mouse_up)
def load_image(self):
# Open file dialog
file_path = filedialog.askopenfilename(
filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.gif")])
if not file_path:
return
# Ask for line type
self.first_line_type = simpledialog.askstring("Line Type",
"First line type (horizontal/vertical):",
initialvalue="horizontal")
if self.first_line_type not in ["horizontal", "vertical"]:
self.first_line_type = "horizontal" # Default
# Load and process image
self.original_img = cv2.imread(file_path)
self.img = self.original_img.copy()
# Resize for display if needed
self.resize_image()
# Draw the first line
self.draw_first_line()
# Initialize second line
height, width = self.img.shape[:2]
center_x, center_y = width // 2, height // 2
if self.first_line_type == "horizontal":
# Second line starts vertical
self.line_end_point = (center_x, center_y - 100)
self.angle = 90
else:
# Second line starts horizontal
self.line_end_point = (center_x + 100, center_y)
self.angle = 90
self.draw_second_line()
self.update_display()
def resize_image(self):
if self.img is None:
return
height, width = self.img.shape[:2]
# Calculate scaling factor to fit in canvas
scale_width = self.canvas_width / width
scale_height = self.canvas_height / height
scale = min(scale_width, scale_height)
if scale < 1:
new_width = int(width * scale)
new_height = int(height * scale)
self.img = cv2.resize(self.original_img, (new_width, new_height))
def draw_first_line(self):
if self.img is None:
return
height, width = self.img.shape[:2]
center_x, center_y = width // 2, height // 2
# Draw first line (red)
if self.first_line_type == "horizontal":
cv2.line(self.img, (0, center_y), (width, center_y), (0, 0, 255), 2)
else: # vertical
cv2.line(self.img, (center_x, 0), (center_x, height), (0, 0, 255), 2)
def draw_second_line(self):
if self.img is None:
return
# Create a copy of the image with first line
self.display_img = self.img.copy()
height, width = self.display_img.shape[:2]
center_x, center_y = width // 2, height // 2
# Draw second line (green)
cv2.line(self.display_img, (center_x, center_y),
self.line_end_point, (0, 255, 0), 2)
# Update angle display
self.angle_var.set(f"{self.angle:.2f}°")
def update_display(self):
if self.display_img is None:
return
# Convert OpenCV image to Tkinter format
img_rgb = cv2.cvtColor(self.display_img, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(img_rgb)
img_tk = ImageTk.PhotoImage(image=img_pil)
# Update canvas
self.canvas.config(width=img_pil.width, height=img_pil.height)
self.canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
self.canvas.image = img_tk # Keep a reference
def calculate_angle(self, point):
if self.img is None:
return 0
height, width = self.img.shape[:2]
center_x, center_y = width // 2, height // 2
# Calculate angle between lines
if self.first_line_type == "horizontal":
# Angle from horizontal line
dx = point[0] - center_x
dy = point[1] - center_y
angle = math.degrees(math.atan2(-dy, dx)) # Negative dy for correct orientation
# Normalize to 0-180
if angle < 0:
angle += 360
# For horizontal reference, we want 0-180 range
if angle > 180:
angle = 360 - angle
else: # vertical
# Angle from vertical line
dx = point[0] - center_x
dy = point[1] - center_y
angle = math.degrees(math.atan2(dx, -dy)) # Negative dy for correct orientation
# Normalize to 0-180
if angle < 0:
angle += 360
# For vertical reference, we want 0-180 range
if angle > 180:
angle = 360 - angle
return angle
def on_mouse_down(self, event):
if self.img is None:
return
self.is_dragging = True
self.on_mouse_drag(event)
def on_mouse_drag(self, event):
if not self.is_dragging or self.img is None:
return
# Update line end point
self.line_end_point = (event.x, event.y)
# Calculate angle
self.angle = self.calculate_angle(self.line_end_point)
# Redraw
self.draw_second_line()
self.update_display()
def on_mouse_up(self, event):
self.is_dragging = False
def copy_angle(self):
self.root.clipboard_clear()
self.root.clipboard_append(self.angle_var.get())
self.root.update() # Required for clipboard to work
if __name__ == "__main__":
root = tk.Tk()
app = AngleMeasurementApp(root)
root.geometry("900x700")
root.mainloop()
Would you like me to explain or break down the code?