Calculate height and width of GIF/JPG files

博客给出了一个名为ImageSize的函数,用于根据文件路径获取GIF或JPG图像的宽度和高度。针对GIF和JPG格式采用不同方式定位尺寸信息,还给出了GIFFile类克隆ImageSize函数逻辑,并添加了GIF格式额外检查以检测不准确的扩展名或类型。

这个是转载
Function ImageSize(fileName As String) As Variant
   ' Given a source file name (path to the GIF or JPG on disk), return an array containing
   ' the width (1st element) and height (2nd element).
   Dim retVal As Variant
   Dim header As String
   Dim f As Integer
   Dim wHi As Variant
   Dim wLo As Variant
   Dim hHi As Variant
   Dim hLo As Variant
   Dim w As Integer ' width of image
   Dim h As Integer ' height of image
   Dim foundMarker As Integer
   
   Redim retVal(2) As Integer
   Redim retVal(Lbound(retVal)+1)     ' Size it so there's 2 entries
   retVal(Lbound(retVal)) = 0
   retVal(Ubound(retVal)) = 0
   f = Freefile()
   On Error Resume Next
   Open fileName For Input As #f
   On Error Goto 0
   If Err <> 0 Then
      ImageSize = retVal  ' File name incorrect - return zero for both the height and width
      Exit Function
   End If
   If Lcase(Right(fileName, 3)) = "gif" Then
      ' GIF's height and width stored in a fixed location
      header = Input(10, f)
      wHi = Mid(header, 8, 1)
      wLo = Mid(header, 7, 1)
      hHi = Mid(header, 10, 1)
      hLo = Mid(header, 9, 1)
      w = Asc(wHi) * 256 + Asc(wLo)
      h = Asc(hHi) * 256 + Asc(hLo)
   Elseif Lcase(Right(fileName, 3)) = "jpg" Then
      ' JPG's stored in a variable location. The code has been verified with JFIF
      ' file format (the most common format)
      On Error Goto EndOfFile     ' In case we run over the file for some reason
      header = Input(2, f)
      If header = Chr$(255) & Chr$(216) Then   ' Must start with hex FF D8
         foundMarker = False   ' Look for the marker that will contain the height and width
         While Not foundMarker
            header = Input(2, f)    ' Grab the next marker
            ' Look for the marker (in hex) FF C0, FF C1, FF C2, or FF C3
            If header = Chr$(255) & Chr$(192) Or header = Chr$(255) & Chr$(193) _
            Or header = Chr$(255) & Chr$(194) Or header = Chr$(255) & Chr$(195) Then
               ' Next two bytes are the length, then a single byte that can be ignored.
               header = Input(3, f)
               ' Next two bytes are the height of the image
               header = Input(2, f)
               hHi = Asc(Midbp(header, 1, 1))
               hLo = Asc(Midbp(header, 2, 1))
               h = hHi * 256 + hLo
               ' Next two bytes are the width of the image
               header = Input(2, f)
               wHi = Asc(Midbp(header, 1, 1))
               wLo = Asc(Midbp(header, 2, 1))
               w = wHi * 256 + wLo
               foundMarker = True     ' Exit the while loop
            Else   ' It's not one of the special markers - skip over it
               header = Input(2, f)   ' Next two bytes are the marker length
               wHi = Asc(Midbp(header, 1, 1))
               wLo = Asc(Midbp(header, 2, 1))
               w = wHi * 256 + wLo
               header = Input(w-2, f) ' Skip over that many bytes (minus the 2 byte length already read)
               w = 0   ' Clear the variable
            End If
         Wend   ' Continue until the marker is found
      End If    ' Ends the check to see if the file starts with FF D8
EndOfFile:
      If Err <> 0 Then
         Err = 0
         Resume AfterError
      End If
   End If   ' Ends the check to see if the format is GIF or JPG
AfterError:
   retVal(Lbound(retVal)) = w
   retVal(Ubound(retVal)) = h
   Close #f
   ImageSize = retVal
End Function


Here's a sample GIFFile class cloning ImageSize() routine original logic:

Private Const GIF_HEADER_LENGTH = 10
Private Const GIF_MARKER = "GIF"
Private Const GIF_ID1 = "87a"
Private Const GIF_ID2= "89a"

Private Class GIFFile

Private m_w As Integer
Private m_h As Integer

Public Property Set fileName As String
Dim h ' GIF file Header: "GIF87a" or GIF89a" followed by logical width & height
h = Me.Header ' Let's check GIF format presence..
If ( Left$( h, 3 ) <> GIF_MARKER ) Then Error 1000, _
|Not a GIF file: Graphical Interchange File "GIF" marker not found|
If ( Mid$( h, 4, 3 ) <> GIF_ID1 And Mid$( h, 4, 3 ) <> GIF_ID2 ) Then Error 1002, _
|Not a GIF file: Graphical Interchange File "87a/89a" identifier not found|
m_w = Asc( Mid( h, 8, 1 ) ) * 256 + Asc( Mid( h, 7, 1 ) ) ' Little-endian Screen Width
m_h = Asc( Mid( h, 10, 1 ) ) * 256 + Asc( Mid( h, 9, 1 ) ) ' Little-endian Screen Height
End Property
Private Property Get Header As Variant
Dim h As Integer
h% = Freefile()
Open Me.Name For Input Shared As #h
Header = Input( GIF_HEADER_LENGTH, #h )
Close #h
End Property
Public Property Get Heigth As Integer
Heigth = m_h
End Property
Public Property Get Width As Integer
Me.Width = m_w
End Property

Public Sub new( fileName As String )
Me.FileName = fileName
End Sub

End Class

I have added GIF format additional checks intended to detect files holding inaccurate extension/type

这段代码第一次拼接预览的时候没有居中显示,帮我变成居中显示 import os import re import sys from PIL import Image, ImageTk import glob import tkinter as tk from tkinter import filedialog, messagebox, ttk import threading class PhotoGridApp: def __init__(self, root): self.root = root self.root.title("图片网格拼接工具") self.root.geometry("1000x800") self.root.minsize(800, 600) self.root.resizable(True, True) # 样式设置 self.style = ttk.Style() self.font_size = 10 self.font_family = "Arial" self.style.configure("TLabel", font=(self.font_family, self.font_size)) self.style.configure("TButton", font=(self.font_family, self.font_size)) self.style.configure("TEntry", font=(self.font_family, self.font_size)) self.style.configure("TLabelframe", font=(self.font_family, self.font_size)) self.style.configure("TLabelframe.Label", font=(self.font_family, self.font_size)) # 主框架 self.main_frame = ttk.Frame(root, padding="20") self.main_frame.pack(fill=tk.BOTH, expand=True) # 源文件夹选择 ttk.Label(self.main_frame, text="源文件夹:").grid(row=0, column=0, sticky=tk.W, pady=5) self.source_folder_var = tk.StringVar() ttk.Entry(self.main_frame, textvariable=self.source_folder_var, width=70).grid(row=0, column=1, sticky=tk.W, pady=5) ttk.Button(self.main_frame, text="浏览...", command=self.browse_source_folder).grid(row=0, column=2, padx=5, pady=5) # 输出文件夹选择 ttk.Label(self.main_frame, text="输出文件夹:").grid(row=1, column=0, sticky=tk.W, pady=5) self.output_folder_var = tk.StringVar() ttk.Entry(self.main_frame, textvariable=self.output_folder_var, width=70).grid(row=1, column=1, sticky=tk.W, pady=5) ttk.Button(self.main_frame, text="浏览...", command=self.browse_output_folder).grid(row=1, column=2, padx=5, pady=5) # 网格方案选择 ttk.Label(self.main_frame, text="网格方案:").grid(row=2, column=0, sticky=tk.W, pady=5) self.grid_option_var = tk.StringVar() self.grid_option_combobox = ttk.Combobox(self.main_frame, textvariable=self.grid_option_var, width=15, state="readonly") self.grid_option_combobox.grid(row=2, column=1, sticky=tk.W, pady=5) self.grid_option_combobox.bind("<<ComboboxSelected>>", self.on_grid_option_selected) # 网格设置 grid_frame = ttk.LabelFrame(self.main_frame, text="网格设置") grid_frame.grid(row=3, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=10) ttk.Label(grid_frame, text="列数:").grid(row=0, column=0, sticky=tk.W, padx=10, pady=5) self.cols_var = tk.IntVar(value=3) ttk.Entry(grid_frame, textvariable=self.cols_var, width=5).grid(row=0, column=1, sticky=tk.W, pady=5) ttk.Label(grid_frame, text="行数:").grid(row=0, column=2, sticky=tk.W, padx=10, pady=5) self.rows_var = tk.IntVar(value=3) ttk.Entry(grid_frame, textvariable=self.rows_var, width=5).grid(row=0, column=3, sticky=tk.W, pady=5) # 图片预览区域 preview_frame = ttk.LabelFrame(self.main_frame, text="拼接预览") preview_frame.grid(row=4, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=10) preview_frame.rowconfigure(0, weight=1) preview_frame.columnconfigure(0, weight=1) # 带滚轮的画布 self.canvas = tk.Canvas(preview_frame, bg="#f0f0f0", highlightthickness=0) scrollbar_y = ttk.Scrollbar(preview_frame, orient="vertical", command=self.canvas.yview) scrollbar_x = ttk.Scrollbar(preview_frame, orient="horizontal", command=self.canvas.xview) self.canvas.configure(yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set) # 布局滚动条和画布 scrollbar_y.pack(side="right", fill="y") scrollbar_x.pack(side="bottom", fill="x") self.canvas.pack(side="left", fill="both", expand=True) # 居中容器框架 self.center_frame = ttk.Frame(self.canvas) self.canvas_window = self.canvas.create_window((0, 0), window=self.center_frame, anchor="center") # 预览内容框架 self.preview_content = ttk.Frame(self.center_frame) self.preview_content.pack(anchor="center", padx=5, pady=5) # 绑定事件 self.center_frame.bind("<Configure>", self.on_center_frame_configure) self.canvas.bind("<Configure>", self.on_canvas_configure) self.canvas.bind_all("<MouseWheel>", self.on_mousewheel) # Windows self.canvas.bind_all("<Button-4>", self.on_mousewheel) # Linux self.canvas.bind_all("<Button-5>", self.on_mousewheel) # Linux # 状态和进度条 self.status_var = tk.StringVar(value="就绪") ttk.Label(self.main_frame, textvariable=self.status_var).grid(row=5, column=0, columnspan=3, sticky=tk.W, pady=5) self.progress_var = tk.DoubleVar(value=0) self.progress_bar = ttk.Progressbar(self.main_frame, variable=self.progress_var, maximum=100) self.progress_bar.grid(row=6, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=5) # 操作按钮 button_frame = ttk.Frame(self.main_frame) button_frame.grid(row=7, column=0, columnspan=3, pady=10) ttk.Button(button_frame, text="预览拼接", command=self.preview_grid).grid(row=0, column=0, padx=10) ttk.Button(button_frame, text="保存拼接", command=self.save_grid).grid(row=0, column=1, padx=10) ttk.Button(button_frame, text="退出", command=root.quit).grid(row=0, column=2, padx=10) # 图片相关变量 self.image_files = [] self.photo_grid = None self.grid_photo = None self.grid_label = None self.grid_options = [] # 设置权重 self.main_frame.rowconfigure(4, weight=1) self.main_frame.columnconfigure(1, weight=1) # 绑定窗口大小变化事件 self.root.bind("<Configure>", self.on_main_window_resize) def browse_source_folder(self): folder = filedialog.askdirectory(title="选择源文件夹") if folder: self.source_folder_var.set(folder) self.calculate_grid_options() def calculate_grid_options(self): source_folder = self.source_folder_var.get() if not source_folder or not os.path.isdir(source_folder): return image_files = glob.glob(os.path.join(source_folder, '*')) self.image_files = [f for f in image_files if f.lower().endswith( ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp', '.tiff', '.tif') )] num_images = len(self.image_files) if num_images == 0: self.status_var.set("提示: 所选文件夹中未找到图片文件") self.grid_option_combobox['values'] = [] self.grid_options = [] return self.status_var.set(f"找到 {num_images} 张图片") self.grid_options = [] max_dim = int(num_images ** 0.5) + 2 for cols in range(1, max_dim + 1): rows = (num_images + cols - 1) // cols self.grid_options.append((cols, rows)) if cols != rows: cols2, rows2 = rows, cols if cols2 * rows2 >= num_images: self.grid_options.append((cols2, rows2)) self.grid_options = list(set(self.grid_options)) self.grid_options.sort(key=lambda x: (x[0] * x[1], abs(x[0] - x[1]))) self.grid_options = self.grid_options[:10] option_texts = [f"{c}列 × {r}行" for c, r in self.grid_options] self.grid_option_combobox['values'] = option_texts if option_texts: self.grid_option_combobox.current(0) self.on_grid_option_selected(None) def on_grid_option_selected(self, event): if not self.grid_options: return selected_index = self.grid_option_combobox.current() if selected_index >= 0 and selected_index < len(self.grid_options): cols, rows = self.grid_options[selected_index] self.cols_var.set(cols) self.rows_var.set(rows) def browse_output_folder(self): folder = filedialog.askdirectory(title="选择输出文件夹") if folder: self.output_folder_var.set(folder) def natural_sort_key(self, s): filename = os.path.basename(s) return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', filename)] def create_photo_grid(self, for_preview=True): try: source_folder = self.source_folder_var.get() cols = self.cols_var.get() rows = self.rows_var.get() if not source_folder or not os.path.isdir(source_folder): self.status_var.set("错误: 请选择有效的源文件夹") return None if cols <= 0 or rows <= 0: self.status_var.set("错误: 列数和行数必须大于0") return None if not self.image_files: image_files = glob.glob(os.path.join(source_folder, '*')) self.image_files = [f for f in image_files if f.lower().endswith( ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp', '.tiff', '.tif') )] if not self.image_files: self.status_var.set("错误: 在所选文件夹中未找到图片文件") return None self.image_files.sort(key=self.natural_sort_key) total_images = cols * rows if len(self.image_files) < total_images: self.status_var.set(f"提示: 需要 {total_images} 张图片,但只找到 {len(self.image_files)} 张") total_images = len(self.image_files) try: with Image.open(self.image_files[0]) as img: img_width, img_height = img.size except Exception as e: self.status_var.set(f"错误: 无法打开第一张图片: {e}") return None canvas_width = img_width * cols canvas_height = img_height * rows canvas = Image.new('RGB', (canvas_width, canvas_height)) total_files = len(self.image_files[:total_images]) for index, image_path in enumerate(self.image_files[:total_images]): try: with Image.open(image_path) as img: if img.size != (img_width, img_height): img = img.resize((img_width, img_height)) row = index // cols col = index % cols x = col * img_width y = row * img_height canvas.paste(img, (x, y)) if for_preview: progress = (index + 1) / total_files * 100 self.root.after(0, lambda p=progress: self.progress_var.set(p)) self.root.after(0, lambda s=f"处理中: {index + 1}/{total_files} - {os.path.basename(image_path)}": self.status_var.set( s)) except Exception as e: self.status_var.set(f"跳过无法处理的图片: {os.path.basename(image_path)} - {e}") except Exception as e: self.status_var.set(f"错误: {str(e)}") messagebox.showerror("错误", f"处理过程中发生错误:\n{str(e)}") return None if for_preview: self.root.after(0, lambda: self.progress_var.set(100)) self.root.after(0, lambda: self.status_var.set(f"预览已生成")) return canvas def preview_grid(self): self.status_var.set("生成预览...") self.progress_var.set(0) for widget in self.preview_content.winfo_children(): widget.destroy() self.grid_label = None self.grid_photo = None thread = threading.Thread(target=self._preview_grid_thread) thread.daemon = True thread.start() def _preview_grid_thread(self): self.photo_grid = self.create_photo_grid(for_preview=True) if self.photo_grid: # 使用延迟执行确保UI完全渲染后再更新预览 self.root.after(100, self.update_preview_size) def on_main_window_resize(self, event=None): if self.photo_grid and event and event.widget == self.root: self.update_preview_size() def update_preview_size(self): if not self.photo_grid: return # 强制刷新尺寸信息 self.canvas.update_idletasks() canvas_width = self.canvas.winfo_width() canvas_height = self.canvas.winfo_height() if canvas_width <= 0: canvas_width = 800 if canvas_height <= 0: canvas_height = 500 img_width, img_height = self.photo_grid.size ratio = min(canvas_width / img_width, canvas_height / img_height) max_ratio = 1.0 min_ratio = 0.1 ratio = max(min(ratio, max_ratio), min_ratio) new_size = (int(img_width * ratio), int(img_height * ratio)) preview_img = self.photo_grid.resize(new_size, Image.LANCZOS) # 使用after确保在下一事件循环中执行,确保UI已准备好 self.root.after(0, self._update_preview_ui, preview_img) def _update_preview_ui(self, preview_img): self.grid_photo = ImageTk.PhotoImage(preview_img) if self.grid_label: self.grid_label.config(image=self.grid_photo) else: self.grid_label = ttk.Label(self.preview_content, image=self.grid_photo) self.grid_label.pack() # 强制刷新布局并居中 self.center_frame.update_idletasks() self.canvas.update_idletasks() # 首次预览使用延迟执行确保居中 if not hasattr(self, 'first_preview_done'): self.root.after(50, self.center_canvas_content) self.first_preview_done = True else: self.center_canvas_content() def center_canvas_content(self): # 强制更新尺寸 self.canvas.update_idletasks() self.center_frame.update_idletasks() canvas_width = self.canvas.winfo_width() canvas_height = self.canvas.winfo_height() frame_width = self.center_frame.winfo_width() frame_height = self.center_frame.winfo_height() # 计算偏移量 x = (canvas_width - frame_width) / 2 y = (canvas_height - frame_height) / 2 # 移动窗口到计算出的位置 self.canvas.coords(self.canvas_window, x, y) self.canvas.configure(scrollregion=self.canvas.bbox("all")) def on_center_frame_configure(self, event): self.canvas.configure(scrollregion=self.canvas.bbox("all")) self.center_canvas_content() def on_canvas_configure(self, event): # 画布尺寸变化时重新居中 self.center_canvas_content() # 如果有图片,调整大小 if self.grid_label: self.update_preview_size() def on_mousewheel(self, event): # 垂直滚动 if event.num == 5 or event.delta < 0: self.canvas.yview_scroll(1, "units") elif event.num == 4 or event.delta > 0: self.canvas.yview_scroll(-1, "units") # 水平滚动(Shift键) if event.state & 0x10: if event.num == 5 or event.delta < 0: self.canvas.xview_scroll(1, "units") elif event.num == 4 or event.delta > 0: self.canvas.xview_scroll(-1, "units") def save_grid(self): if not self.photo_grid: messagebox.showinfo("提示", "请先预览拼接效果") return output_folder = self.output_folder_var.get() or os.getcwd() if not os.path.exists(output_folder): os.makedirs(output_folder) total_images = len(self.image_files) output_filename = f"{total_images}格.jpg" output_path = os.path.join(output_folder, output_filename) counter = 1 while os.path.exists(output_path): output_filename = f"{total_images}_{counter}.jpg" output_path = os.path.join(output_folder, output_filename) counter += 1 try: self.photo_grid.save(output_path) self.status_var.set(f"成功保存图片网格: {output_path}") messagebox.showinfo("成功", f"图片网格已成功保存!\n位置: {output_path}") except Exception as e: self.status_var.set(f"错误: 无法保存图片 - {str(e)}") messagebox.showerror("错误", f"保存图片时发生错误:\n{str(e)}") def main(): root = tk.Tk() app = PhotoGridApp(root) root.mainloop() if __name__ == "__main__": main()
07-15
对于该段代码:# Import necessary libraries for GUI and image processing import tkinter as tk from tkinter import filedialog, ttk, messagebox import numpy as np from PIL import Image, ImageTk, ImageDraw import math import time # Class to display algorithm information in a separate window class AlgorithmInfoWindow: def __init__(self, parent): # Create a new window with fade-in effect self.window = tk.Toplevel(parent) self.window.title("Canny Edge Detection - Algorithm Information") self.window.geometry("800x600") self.window.configure(bg='#f0f0f0') # Make the window float on top of the parent window self.window.transient(parent) self.window.grab_set() # Set initial transparency for fade-in effect self.window.attributes('-alpha', 0.0) # Configure styles for labels and frames style = ttk.Style() style.configure('Info.TLabel', font=('Helvetica', 11), background='#f0f0f0', wraplength=700) style.configure('InfoTitle.TLabel', font=('Helvetica', 14, 'bold'), background='#f0f0f0', foreground='#2c3e50') style.configure('InfoSection.TFrame', background='#ffffff', relief='solid') style.configure('Hover.TFrame', background='#e8f0fe') # Create main frame with custom canvas for smooth scrolling main_frame = ttk.Frame(self.window, style='InfoSection.TFrame') main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20) # Create canvas with custom scrolling self.canvas = tk.Canvas(main_frame, bg='#ffffff', highlightthickness=0, relief='flat') scrollbar = ttk.Scrollbar(main_frame, orient="vertical", command=self.smooth_scroll) self.content_frame = ttk.Frame(self.canvas, style='InfoSection.TFrame') # Configure scrolling self.canvas.configure(yscrollcommand=scrollbar.set) # Pack scrollbar and canvas scrollbar.pack(side="right", fill="y") self.canvas.pack(side="left", fill="both", expand=True) # Create window in canvas self.canvas_frame = self.canvas.create_window( (0, 0), window=self.content_frame, anchor="nw", width=self.canvas.winfo_reqwidth() ) # Add sections with animation delays self.sections = [ ("Canny Edge Detection Algorithm", """The Canny edge detection algorithm is a multi-stage algorithm developed by John F. Canny in 1986. It is considered one of the most robust edge detection algorithms."""), ("1. Grayscale Conversion", """Convert the image to grayscale using weighted sum: gray = 0.2989 * R + 0.5870 * G + 0.1140 * B This weights are based on human perception of color."""), ("2. Gaussian Blur", """Apply Gaussian blur to reduce noise: - Create 5x5 Gaussian kernel using the formula: G(x,y) = (1/2πσ²)e^(-(x²+y²)/2σ²) - Convolve image with kernel - Reduces noise while preserving edges"""), ("3. Gradient Calculation", """Calculate intensity gradients: - Apply Sobel operators in x and y directions - Find gradient magnitude: √(Gx² + Gy²) - Find gradient direction: θ = arctan(Gy/Gx) Sobel operators: X = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] Y = [[-1, -2, -1], [ 0, 0, 0], [ 1, 2, 1]]"""), ("4. Non-Maximum Suppression", """Thin edges by suppressing non-maximum values: 1. Round gradient direction to nearest 45° 2. Compare with pixels in gradient direction 3. Suppress if not local maximum This creates thin, precise edges."""), ("5. Double Thresholding", """Identify strong and weak edges: - High threshold (strong): typically 0.15 * max - Low threshold (weak): typically 0.05 * max Creates three categories: - Strong edges (keep) - Weak edges (evaluate) - Non-edges (discard)"""), ("6. Edge Tracking by Hysteresis", """Connect edges using hysteresis: 1. Start with strong edges 2. Recursively add connected weak edges 3. Remove isolated weak edges This creates continuous edge lines.""") ] # Add sections with animation self.section_frames = [] for i, (title, content) in enumerate(self.sections): self.window.after(i * 100, lambda t=title, c=content: self.add_section_with_animation(t, c)) # Configure canvas scrolling self.content_frame.bind('<Configure>', self.on_frame_configure) self.canvas.bind('<Configure>', self.on_canvas_configure) # Bind mouse wheel for smooth scrolling self.canvas.bind_all('<MouseWheel>', self.on_mousewheel) # Close button with hover effect self.close_btn = ttk.Button(self.window, text="Close", command=self.close_with_animation, style='Custom.TButton') self.close_btn.pack(pady=10) # Start fade-in animation self.fade_in() def fade_in(self, alpha=0.0): """Animate window fade in""" if alpha < 1.0: alpha += 0.1 self.window.attributes('-alpha', alpha) self.window.after(20, lambda: self.fade_in(alpha)) def fade_out(self, alpha=1.0): """Animate window fade out""" if alpha > 0: alpha -= 0.1 self.window.attributes('-alpha', alpha) self.window.after(20, lambda: self.fade_out(alpha)) else: self.window.destroy() def close_with_animation(self): """Close window with fade-out animation""" self.fade_out() def add_section_with_animation(self, title, content): """Add a section with slide-in animation""" frame = ttk.Frame(self.content_frame, style='InfoSection.TFrame') frame.pack(fill=tk.X, pady=10, padx=10) frame.pack_propagate(False) # Prevent size changes # Create content title_label = ttk.Label(frame, text=title, style='InfoTitle.TLabel') title_label.pack(anchor='w', pady=(5, 0)) content_label = ttk.Label(frame, text=content, style='Info.TLabel') content_label.pack(anchor='w', pady=(5, 10)) # Add hover effect frame.bind('<Enter>', lambda e: self.on_section_hover(frame, True)) frame.bind('<Leave>', lambda e: self.on_section_hover(frame, False)) # Animate frame height frame.update() required_height = title_label.winfo_reqheight() + content_label.winfo_reqheight() + 20 frame.configure(height=1) self.animate_frame_height(frame, required_height) self.section_frames.append(frame) def animate_frame_height(self, frame, target_height, current_height=1): """Animate frame height smoothly""" if current_height < target_height: current_height += (target_height - current_height) * 0.2 if current_height < target_height - 1: frame.configure(height=int(current_height)) self.window.after(10, lambda: self.animate_frame_height(frame, target_height, current_height)) else: frame.configure(height=target_height) frame.pack_propagate(True) def on_section_hover(self, frame, entering): """Handle section hover effect""" frame.configure(style='Hover.TFrame' if entering else 'InfoSection.TFrame') def smooth_scroll(self, *args): """Implement smooth scrolling""" if len(args) > 1: self.canvas.yview_moveto(args[1]) else: # Use smoother scrolling with acceleration amount = int(args[0]) # Apply scrolling with acceleration effect if amount != 0: for i in range(3): factor = 0.7 ** i # Decreasing factor for deceleration scroll_amount = int(amount * factor) if amount * factor >= 1 or amount * factor <= -1 else amount self.window.after(i * 5, lambda a=scroll_amount: self.canvas.yview_scroll(a, 'units')) def on_mousewheel(self, event): """Handle smooth mousewheel scrolling with improved animation""" # Get the delta value and normalize it delta = -1 * (event.delta // 120) # Use more steps with smaller increments for smoother animation steps = 15 # Increased steps for smoother animation # Apply scrolling with cubic deceleration curve for i in range(steps): factor = 1 - (i / steps) ** 3 # Cubic deceleration for smoother stop scroll_amount = int(delta * 2 * factor) # Multiply by 2 for better initial momentum scroll_amount = max(1, scroll_amount) if scroll_amount > 0 else min(-1, scroll_amount) # Apply with increasing delay for natural deceleration self.window.after(i * 5, lambda a=scroll_amount: self.canvas.yview_scroll(a, 'units')) def on_frame_configure(self, event=None): """Reset scroll region when content frame size changes""" self.canvas.configure(scrollregion=self.canvas.bbox("all")) def on_canvas_configure(self, event): """Update canvas window size when canvas is resized""" self.canvas.itemconfig(self.canvas_frame, width=event.width) # Main class for the Canny Edge Detection tool class CannyEdgeDetector: def __init__(self): # Initialize the main window self.window = tk.Tk() self.window.title("Canny Edge Detection Tool") self.window.geometry("1200x800") self.window.configure(bg='#f0f0f0') # Set theme for the application style = ttk.Style() style.theme_use('clam') # Configure styles for frames and buttons style.configure('Custom.TFrame', background='#f0f0f0') style.configure('Custom.TButton', padding=10, font=('Helvetica', 10, 'bold')) style.configure('Title.TLabel', font=('Helvetica', 28, 'bold'), # Increased font size background='#f0f0f0', foreground='#2c3e50') style.configure('Subtitle.TLabel', font=('Helvetica', 12), background='#f0f0f0', foreground='#34495e') style.configure('Progress.Horizontal.TProgressbar', background='#2ecc71', troughcolor='#ecf0f1', bordercolor='#bdc3c7') # Create main canvas for scrolling self.main_canvas = tk.Canvas(self.window, bg='#f0f0f0', highlightthickness=0) self.scrollbar = ttk.Scrollbar(self.window, orient="vertical", command=self.smooth_scroll) self.main_canvas.configure(yscrollcommand=self.scrollbar.set) # Pack scrollbar and canvas self.scrollbar.pack(side="right", fill="y") self.main_canvas.pack(side="left", fill="both", expand=True) # Create main frame inside canvas self.main_frame = ttk.Frame(self.main_canvas, padding="20", style='Custom.TFrame') self.canvas_frame = self.main_canvas.create_window( (0, 0), window=self.main_frame, anchor="nw", width=self.main_canvas.winfo_reqwidth() ) # Title and buttons frame title_frame = ttk.Frame(self.main_frame, style='Custom.TFrame') title_frame.grid(row=0, column=0, columnspan=2, sticky='ew') title_frame.grid_columnconfigure(0, weight=1) # Make middle column expandable # Title (centered) self.title_label = ttk.Label(title_frame, text="Canny Edge Detection", style='Title.TLabel', anchor='center') self.title_label.grid(row=0, column=0, pady=(0, 20), sticky='ew') # Centered title # Button frame self.button_frame = ttk.Frame(self.main_frame, style='Custom.TFrame') self.button_frame.grid(row=1, column=0, columnspan=2, pady=(0, 20)) # Buttons (all in one line) self.choose_btn = ttk.Button(self.button_frame, text="Choose Image", command=self.load_image, style='Custom.TButton') self.choose_btn.grid(row=0, column=0, padx=10) self.process_btn = ttk.Button(self.button_frame, text="Process", command=self.process_image_with_progress, style='Custom.TButton') self.process_btn.grid(row=0, column=1, padx=10) self.reset_btn = ttk.Button(self.button_frame, text="Reset", command=self.reset_images, style='Custom.TButton') self.reset_btn.grid(row=0, column=2, padx=10) # Info button (next to reset button) self.info_btn = ttk.Button(self.button_frame, text="ℹ️ Algorithm Info", command=self.show_algorithm_info, style='Custom.TButton') self.info_btn.grid(row=0, column=3, padx=10) # Progress frame self.progress_frame = ttk.Frame(self.main_frame, style='Custom.TFrame') self.progress_frame.grid(row=2, column=0, columnspan=2, pady=(0, 20)) # Progress bar self.progress_var = tk.DoubleVar() self.progress_bar = ttk.Progressbar(self.progress_frame, variable=self.progress_var, maximum=100, mode='determinate', length=400, style='Progress.Horizontal.TProgressbar') self.progress_bar.grid(row=0, column=0, padx=(0, 10)) # Progress percentage label self.progress_label = ttk.Label(self.progress_frame, text="0%", style='Subtitle.TLabel') self.progress_label.grid(row=0, column=1) # Hide progress frame initially self.progress_frame.grid_remove() # Status label self.status_label = ttk.Label(self.main_frame, text="", style='Subtitle.TLabel') self.status_label.grid(row=3, column=0, columnspan=2, pady=(0, 20)) # Image frames self.create_image_frame("Original Image", 4, 0) self.create_image_frame("Edge Detected Image", 4, 1) # Initialize variables self.current_image = None self.processed_image = None # Load default square image self.create_default_square_image() # Configure grid weights for responsive layout self.window.grid_rowconfigure(0, weight=1) self.window.grid_columnconfigure(0, weight=1) self.main_frame.grid_rowconfigure(4, weight=1) # Make image frames expandable self.main_frame.grid_columnconfigure(0, weight=1) self.main_frame.grid_columnconfigure(1, weight=1) # Bind events for smooth scrolling with improved responsiveness self.main_canvas.bind('<Configure>', self.on_canvas_configure) self.main_frame.bind('<Configure>', self.on_frame_configure) self.main_canvas.bind_all('<MouseWheel>', self.on_mousewheel) # Ensure buttons stay visible during processing self.button_frame.lift() self.progress_frame.lift() def create_default_square_image(self): """Create default square image as shown in screenshot""" # Create larger image to better fill the frame size = 400 # Increased size img = Image.new('RGB', (size, size), 'black') draw = ImageDraw.Draw(img) # Calculate sizes for squares outer_size = int(size * 0.6) # 60% of total size inner_size = int(outer_size * 0.3) # 30% of outer square # Calculate positions outer_offset = (size - outer_size) // 2 outer_box = [(outer_offset, outer_offset), (outer_offset + outer_size, outer_offset + outer_size)] # Draw outer white square draw.rectangle(outer_box, fill='white') # Calculate inner square position inner_offset = (size - inner_size) // 2 inner_box = [(inner_offset, inner_offset), (inner_offset + inner_size, inner_offset + inner_size)] # Draw inner black square draw.rectangle(inner_box, fill='black') self.current_image = np.array(img) self.display_image(img, self.original_image_label) def show_algorithm_info(self): """Show algorithm information window""" AlgorithmInfoWindow(self.window) def reset_images(self): """Reset to default square image""" self.create_default_square_image() if self.processed_image_label: self.processed_image_label.configure(image='') self.hide_progress() self.status_label.config(text="Reset complete") def update_progress(self, value, status_text): """Update progress bar, percentage and status text""" self.progress_var.set(value) self.progress_label.config(text=f"{int(value)}%") self.status_label.config(text=status_text) self.window.update() def hide_progress(self): """Hide progress elements""" self.progress_frame.grid_remove() self.status_label.config(text="") self.progress_var.set(0) self.progress_label.config(text="0%") def create_image_frame(self, title, row, column): """Create a frame for displaying images with a title and border""" # Create main frame with fixed size and gray background frame = ttk.Frame(self.main_frame, padding="10", relief="solid", borderwidth=1) frame.grid(row=row, column=column, padx=5, pady=(0, 10), sticky="nsew") # Force both frames to have identical width self.main_frame.grid_columnconfigure(0, weight=1, uniform="equal") # Use uniform to ensure equal size self.main_frame.grid_columnconfigure(1, weight=1, uniform="equal") # Use uniform to ensure equal size # Configure frame size with optimal dimensions for small screens frame.grid_propagate(False) # Prevent frame from resizing to content frame.configure(width=350, height=350) # Reduced dimensions for better small screen compatibility # Title with enhanced styling style = ttk.Style() style.configure('FrameTitle.TLabel', font=('Helvetica', 14, 'bold'), foreground='#2c3e50', background='#e0e0e0', padding=5) # Title container frame with background title_container = ttk.Frame(frame, style='Custom.TFrame') title_container.pack(fill="x", pady=(0, 10)) title_container.configure(height=40) title_label = ttk.Label(title_container, text=title, style='FrameTitle.TLabel', anchor='center') title_label.pack(fill="x", expand=True) # Create image container frame with gray background image_container = ttk.Frame(frame, style='Custom.TFrame') image_container.pack(expand=True, fill="both", padx=5, pady=5) image_container.configure(width=480, height=430) # Image label with gray background image_label = ttk.Label(image_container) image_label.pack(expand=True, fill="both") # Save button container save_container = ttk.Frame(frame, style='Custom.TFrame') save_container.pack(fill="x", pady=(5, 0)) # Save button with enhanced style style.configure('Save.TButton', font=('Helvetica', 10), padding=5) save_btn = ttk.Button(save_container, text="Save Image", style='Save.TButton', command=lambda: self.save_image(column)) save_btn.pack(pady=5) if column == 0: self.original_image_label = image_label else: self.processed_image_label = image_label def display_image(self, image, label): """ Display numpy array image in GUI label Args: image: numpy array of image data label: target label widget for display Maintains aspect ratio while fitting to display area """ if image: # Use fixed dimensions for both frames to ensure they're identical frame_width = 480 frame_height = 430 # Calculate scaling ratio while preserving aspect ratio img_width, img_height = image.size width_ratio = frame_width / img_width height_ratio = frame_height / img_height scale_ratio = min(width_ratio, height_ratio) # Calculate new size new_width = int(img_width * scale_ratio) new_height = int(img_height * scale_ratio) # Resize image resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS) # Create new image with gray background final_image = Image.new('RGB', (frame_width, frame_height), '#f0f0f0') # Calculate position to center the image x_offset = (frame_width - new_width) // 2 y_offset = (frame_height - new_height) // 2 # Paste resized image onto background final_image.paste(resized_image, (x_offset, y_offset)) # Convert to PhotoImage and display photo = ImageTk.PhotoImage(final_image) label.configure(image=photo) label.image = photo # Keep reference def save_image(self, image_type): """Save the image to disk""" if image_type == 0 and self.current_image is not None: image_to_save = Image.fromarray(self.current_image) title = "Save Original Image" elif image_type == 1 and hasattr(self, 'processed_image'): image_to_save = Image.fromarray((self.processed_image * 255).astype(np.uint8)) title = "Save Edge Detected Image" else: messagebox.showwarning("Warning", "No image to save!") return # Ask for save location file_path = filedialog.asksaveasfilename( title=title, defaultextension=".png", filetypes=[ ("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*") ] ) if file_path: try: image_to_save.save(file_path) messagebox.showinfo("Success", "Image saved successfully!") except Exception as e: messagebox.showerror("Error", f"Failed to save image: {str(e)}") def process_image_with_progress(self): """ Execute edge detection pipeline with progress tracking Steps: 1. Input validation 2. Grayscale conversion 3. Gaussian blur 4. Gradient calculation 5. Non-max suppression 6. Double thresholding 7. Hysteresis edge tracking """ if self.current_image is None: messagebox.showwarning("Warning", "Please select an image first!") return self.progress_frame.grid() # Show progress frame self.status_label.grid() # Show status label try: # Convert to grayscale self.update_progress(20, "Converting to grayscale...") time.sleep(0.3) # Simulate processing time gray_image = self.to_grayscale(self.current_image) # Apply Gaussian blur self.update_progress(40, "Applying Gaussian blur...") time.sleep(0.3) blurred = self.apply_gaussian_blur(gray_image) # Calculate gradients self.update_progress(60, "Calculating gradients...") time.sleep(0.3) gradient_magnitude, gradient_direction = self.sobel_filters(blurred) # Apply non-maximum suppression self.update_progress(70, "Applying non-maximum suppression...") time.sleep(0.3) suppressed = self.non_maximum_suppression(gradient_magnitude, gradient_direction) # Apply double threshold self.update_progress(80, "Applying double threshold...") time.sleep(0.3) strong_edges, weak_edges = self.double_threshold(suppressed) # Apply hysteresis self.update_progress(90, "Applying hysteresis...") time.sleep(0.3) final_edges = self.hysteresis(strong_edges, weak_edges) # Store processed image self.processed_image = final_edges # Display result self.update_progress(100, "Complete!") display_image = Image.fromarray((final_edges * 255).astype(np.uint8)) self.display_image(display_image, self.processed_image_label) # Hide progress elements after a delay self.window.after(1000, self.hide_progress) except Exception as e: messagebox.showerror("Error", f"An error occurred: {str(e)}") self.hide_progress() def load_image(self): """ Load an image through file dialog Supported formats: JPEG, PNG, BMP Updates original image display Handles common file errors """ file_path = filedialog.askopenfilename( filetypes=[ ("Image files", "*.jpg;*.jpeg;*.png;*.bmp;*.gif"), ("All files", "*.*") ] ) if file_path: try: # Load image image = Image.open(file_path) # Convert to RGB if necessary if image.mode != 'RGB': image = image.convert('RGB') # Store original image for processing self.current_image = np.array(image) # Display image with proper scaling self.display_image(image, self.original_image_label) # Clear processed image if self.processed_image_label: self.processed_image_label.configure(image='') self.status_label.config(text="Image loaded successfully") except Exception as e: messagebox.showerror("Error", f"Failed to load image: {str(e)}") self.status_label.config(text="Failed to load image") def to_grayscale(self, image): """Convert RGB image to grayscale using manual implementation""" if len(image.shape) == 3: return np.dot(image[..., :3], [0.2989, 0.5870, 0.1140]).astype(np.float32) return image def gaussian_kernel(self, size, sigma=1.4): """Generate Gaussian kernel manually""" kernel = np.zeros((size, size)) center = size // 2 for x in range(size): for y in range(size): x_dist = x - center y_dist = y - center kernel[x, y] = (1 / (2 * np.pi * sigma ** 2)) * np.exp(-(x_dist ** 2 + y_dist ** 2) / (2 * sigma ** 2)) return kernel / np.sum(kernel) def apply_gaussian_blur(self, image, kernel_size=5): """Apply Gaussian blur manually""" kernel = self.gaussian_kernel(kernel_size) padding = kernel_size // 2 padded = np.pad(image, padding, mode='edge') output = np.zeros_like(image) for i in range(image.shape[0]): for j in range(image.shape[1]): output[i, j] = np.sum( padded[i:i + kernel_size, j:j + kernel_size] * kernel ) return output def sobel_filters(self, image): """Apply Sobel filters manually""" Gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Gy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) padding = 1 padded = np.pad(image, padding, mode='edge') gradient_x = np.zeros_like(image) gradient_y = np.zeros_like(image) for i in range(image.shape[0]): for j in range(image.shape[1]): gradient_x[i, j] = np.sum( padded[i:i + 3, j:j + 3] * Gx ) gradient_y[i, j] = np.sum( padded[i:i + 3, j:j + 3] * Gy ) gradient_magnitude = np.sqrt(gradient_x ** 2 + gradient_y ** 2) gradient_direction = np.arctan2(gradient_y, gradient_x) return gradient_magnitude, gradient_direction def non_maximum_suppression(self, gradient_magnitude, gradient_direction): """Apply non-maximum suppression""" height, width = gradient_magnitude.shape output = np.zeros_like(gradient_magnitude) # Convert angles from radians to degrees angle = gradient_direction * 180 / np.pi angle[angle < 0] += 180 for i in range(1, height - 1): for j in range(1, width - 1): q = 255 r = 255 # Angle 0 if (0 <= angle[i, j] < 22.5) or (157.5 <= angle[i, j] <= 180): q = gradient_magnitude[i, j + 1] r = gradient_magnitude[i, j - 1] # Angle 45 elif (22.5 <= angle[i, j] < 67.5): q = gradient_magnitude[i + 1, j - 1] r = gradient_magnitude[i - 1, j + 1] # Angle 90 elif (67.5 <= angle[i, j] < 112.5): q = gradient_magnitude[i + 1, j] r = gradient_magnitude[i - 1, j] # Angle 135 elif (112.5 <= angle[i, j] < 157.5): q = gradient_magnitude[i - 1, j - 1] r = gradient_magnitude[i + 1, j + 1] if (gradient_magnitude[i, j] >= q) and (gradient_magnitude[i, j] >= r): output[i, j] = gradient_magnitude[i, j] else: output[i, j] = 0 return output def double_threshold(self, image, low_ratio=0.05, high_ratio=0.15): """Apply double threshold""" high_threshold = image.max() * high_ratio low_threshold = high_threshold * low_ratio strong_edges = (image >= high_threshold) weak_edges = (image >= low_threshold) & (image < high_threshold) return strong_edges, weak_edges def hysteresis(self, strong_edges, weak_edges): """Apply hysteresis to connect edges""" height, width = strong_edges.shape output = np.copy(strong_edges) dx = [-1, -1, -1, 0, 0, 1, 1, 1] dy = [-1, 0, 1, -1, 1, -1, 0, 1] # Iterate until no more changes while True: previous = np.copy(output) for i in range(1, height - 1): for j in range(1, width - 1): if weak_edges[i, j]: # Check if any neighbor is a strong edge for k in range(8): if output[i + dx[k], j + dy[k]]: output[i, j] = True break if np.array_equal(previous, output): break return output def on_mousewheel(self, event): """Handle smooth mousewheel scrolling with improved animation""" # Get the delta value and normalize it delta = -1 * (event.delta // 120) # Use more steps with smaller increments for smoother animation steps = 15 # Increased number of steps for even smoother scrolling # Apply scrolling with acceleration and deceleration for i in range(steps): # Calculate a smooth deceleration curve factor = 1 - (i / steps) ** 2 # Quadratic deceleration for natural feel scroll_amount = max(1, int(delta * factor)) if delta > 0 else min(-1, int(delta * factor)) # Apply with increasing delay for natural deceleration self.window.after(i * 4, lambda a=scroll_amount: self.main_canvas.yview_scroll(a, 'units')) def smooth_scroll(self, *args): """Implement smooth scrolling""" if len(args) > 1: self.main_canvas.yview_moveto(args[1]) else: # Use smoother scrolling with acceleration amount = int(args[0]) # Apply scrolling with acceleration effect if amount != 0: for i in range(5): # Increased range for smoother scrolling factor = 0.8 ** i # Adjusted factor for better deceleration scroll_amount = int(amount * factor) if amount * factor >= 1 or amount * factor <= -1 else amount self.window.after(i * 4, lambda a=scroll_amount: self.main_canvas.yview_scroll(a, 'units')) def on_frame_configure(self, event=None): """Reset scroll region when content frame size changes""" self.main_canvas.configure(scrollregion=self.main_canvas.bbox("all")) # Ensure the canvas is large enough to accommodate all content self.main_frame.update_idletasks() def on_canvas_configure(self, event): """Update canvas window size when canvas is resized""" self.main_canvas.itemconfig(self.canvas_frame, width=event.width) # Ensure the canvas window is properly sized for future content additions self.main_canvas.configure(width=event.width) def run(self): self.window.mainloop() if __name__ == "__main__": app = CannyEdgeDetector() app.run()如何在训练阶段,使用农田边缘区域的掩码标注数据,限定模型仅在边缘区域进行目标检测,忽略农田内部区域。
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值