当前位置: 首页 > news >正文

Tkinter软件——显示txt标签的目标水平边框图像

代码:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import cv2
from PIL import Image, ImageTk
import osclass ImageBoxApp:def __init__(self, master):self.master = masterself.master.title("Image Box Drawer")# 第一行:加载TXT文件夹和TXT路径显示self.txt_frame = tk.Frame(master)self.txt_frame.pack()self.load_txt_button = tk.Button(self.txt_frame, text="加载TXT文件夹", command=self.load_txt_folder)self.load_txt_button.pack(side=tk.LEFT)self.txt_path_label = tk.Label(self.txt_frame, text="TXT 文件夹路径: ")self.txt_path_label.pack(side=tk.LEFT)# 第二行:加载图像文件夹和图像路径显示self.img_frame = tk.Frame(master)self.img_frame.pack()self.load_img_button = tk.Button(self.img_frame, text="加载图像文件夹", command=self.load_img_folder)self.load_img_button.pack(side=tk.LEFT)self.img_path_label = tk.Label(self.img_frame, text="图像文件夹路径: ")self.img_path_label.pack(side=tk.LEFT)# 图像显示self.image_label = tk.Label(master)self.image_label.pack()# 最后一行:上一张、下一张按钮和当前图像路径显示self.button_frame = tk.Frame(master)self.button_frame.pack()self.prev_button = tk.Button(self.button_frame, text="上一张", command=self.prev_image)self.prev_button.pack(side=tk.LEFT)self.next_button = tk.Button(self.button_frame, text="下一张", command=self.next_image)self.next_button.pack(side=tk.LEFT)self.image_path_label = tk.Label(self.button_frame, text="当前图像路径: ")self.image_path_label.pack(side=tk.LEFT)self.txt_folder = Noneself.img_folder = Noneself.txt_files = []self.img_files = []self.current_index = 0def load_txt_folder(self):self.txt_folder = filedialog.askdirectory()if not self.txt_folder:returnself.txt_files = [f for f in os.listdir(self.txt_folder) if f.endswith('.txt')]if not self.txt_files:messagebox.showerror("错误", "该文件夹中没有TXT文件")else:self.txt_path_label.config(text=f"TXT 文件夹路径: {self.txt_folder}")def load_img_folder(self):self.img_folder = filedialog.askdirectory()if not self.img_folder:returnself.img_files = [f for f in os.listdir(self.img_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]if not self.img_files:messagebox.showerror("错误", "该文件夹中没有图像文件")else:self.img_path_label.config(text=f"图像文件夹路径: {self.img_folder}")self.current_index = 0self.show_image()def show_image(self):if self.img_files and self.txt_files:img_path = os.path.join(self.img_folder, self.img_files[self.current_index])txt_path = os.path.join(self.txt_folder, self.txt_files[self.current_index])self.image_path_label.config(text=f"当前图像路径: {img_path}")self.draw_boxes(img_path, txt_path)def draw_boxes(self, image_path, txt_path):image = cv2.imread(image_path)height, width, _ = image.shapewith open(txt_path, 'r') as f:lines = f.readlines()for line in lines:parts = line.strip().split()class_id = int(parts[0])x_center = float(parts[1]) * widthy_center = float(parts[2]) * heightbox_width = float(parts[3]) * widthbox_height = float(parts[4]) * heightx1 = int(x_center - box_width / 2)y1 = int(y_center - box_height / 2)x2 = int(x_center + box_width / 2)y2 = int(y_center + box_height / 2)cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)image = Image.fromarray(image)image = ImageTk.PhotoImage(image)self.image_label.config(image=image)self.image_label.image = imagedef prev_image(self):if self.img_files and self.txt_files:if self.current_index == 0:messagebox.showinfo("提示", "已经是第一张图像")else:self.current_index -= 1self.show_image()def next_image(self):if self.img_files and self.txt_files:if self.current_index == len(self.img_files) - 1:messagebox.showinfo("提示", "已经是最后一张图像")else:self.current_index += 1self.show_image()if __name__ == "__main__":root = tk.Tk()app = ImageBoxApp(root)root.mainloop()

测试:

http://www.xdnf.cn/news/740035.html

相关文章:

  • StringBulder的底层原理?
  • 多功能文档处理工具推荐
  • YOLOv8目标检测实战-(TensorRT原生API搭建网络和使用Parser搭建网络)
  • 海外DeepLink方案复杂?用openinstall一站式链接世界
  • Dart语言学习指南「专栏简介」
  • Display Driver Uninstaller(DDU卸载显卡驱动工具)官网下载
  • 扫地机器人苦寻新引擎,大疆们却已攻入腹地
  • 龙舟竞渡与芯片制造的共通逻辑:华芯邦的文化破局之道
  • 使用基于Xsens惯性传感器的动作捕捉技术测量人体工程学
  • 【DRAM存储器五十二】LPDDR5介绍--MR17设置的SOC ODT是啥?
  • 文心快码参编国内首个软件开发智能体技术规范
  • 回车键为什么叫做“回车键”?
  • 雷达目标起伏特性简析
  • vue3实现鼠标悬浮div动画效果
  • muduo库的初步认识和基本使用,创建一个简单查询单词服务系统
  • 二重积分的线性性
  • 【吾爱】逆向实战crackme160学习记录(一)
  • 辅助脚本-通用开发工作区目录结构生成脚本解析与实践指南
  • 软件测试的分类
  • 半导体行业重金属废水处理技术革新:从传统沉淀到复合工艺的突破
  • 大话软工笔记—分离原理
  • 如何做好一份技术文档:构建知识传递的精准航海图
  • 如何在腾讯云 OpenCloudOS 上安装 Docker 和 Docker Compose
  • shell脚本打包成可以在麒麟桌面操作系统上使用的deb包
  • QPushButton设置菜单
  • 408考研逐题详解:2009年第25题
  • 1、python代码实现与大模型的问答交互
  • iEKF的二维应用实例
  • 信息茧房—警惕低头族作茧自缚
  • Linux研学-环境搭建