利用python装饰器,客制化图像处理功能

2024-08-21 12:04

本文主要是介绍利用python装饰器,客制化图像处理功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • detection_plugin.py
  • main.py
  • plugin_interface.py

有时间再写

detection_plugin.py

# detection_plugin.py
import cv2
import numpy as np
from plugin_interface import ImageProcessingPluginclass EdgeDetectionPlugin(ImageProcessingPlugin):def process_image(self, image_path):# 读取图像image = cv2.imread(image_path, cv2.IMREAD_COLOR)if image is None:return None, "Image not found"# 转换为灰度图像gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 边缘检测edges = cv2.Canny(gray, 100, 200)return edges, None

main.py

import sys
import importlib.util
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QPushButton, QLabel, QVBoxLayout, QWidget, QMessageBox
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
from plugin_interface import ImageProcessingPluginclass ImageViewer(QWidget):def __init__(self):super().__init__()self.image_label = QLabel()layout = QVBoxLayout()layout.addWidget(self.image_label)self.setLayout(layout)def load_image(self, pixmap):self.image_label.setPixmap(pixmap)def load_image_from_path(self, file_path):pixmap = QPixmap(file_path)self.load_image(pixmap)def display_edges(self, edges):# 转换边缘检测结果为 QPixmap 显示height, width = edges.shapeqimage = QImage(edges.data, width, height, width, QImage.Format_Grayscale8)pixmap = QPixmap.fromImage(qimage)self.load_image(pixmap)class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle('Image Viewer with Plugin')self.setGeometry(100, 100, 800, 600)self.current_plugin = Noneself.file_path = Noneself.plugin_path = Nonecentral_widget = QWidget()self.setCentralWidget(central_widget)layout = QVBoxLayout(central_widget)# 图像查看器self.image_viewer = ImageViewer()layout.addWidget(self.image_viewer)# 加载图像按钮self.load_button = QPushButton('Load Image')self.load_button.clicked.connect(self.load_image)layout.addWidget(self.load_button)# 执行检测按钮self.detect_button = QPushButton('Detect Edges')self.detect_button.clicked.connect(self.detect_edges)layout.addWidget(self.detect_button)# 状态标签self.status_label = QLabel('Status: ')layout.addWidget(self.status_label)# 选择插件按钮self.load_plugin_button = QPushButton('Load Plugin')self.load_plugin_button.clicked.connect(self.load_plugin)layout.addWidget(self.load_plugin_button)def load_image(self):self.file_path, _ = QFileDialog.getOpenFileName(self, 'Open Image File', '', 'Images (*.png *.xpm *.jpg *.bmp)')if self.file_path:self.image_viewer.load_image_from_path(self.file_path)self.status_label.setText(f'Status: Loaded {self.file_path}')def load_plugin(self):# 选择插件文件self.plugin_path, _ = QFileDialog.getOpenFileName(self, 'Select Plugin File', '', 'Python Files (*.py)')if self.plugin_path:try:# 动态加载插件spec = importlib.util.spec_from_file_location("plugin", self.plugin_path)plugin_module = importlib.util.module_from_spec(spec)spec.loader.exec_module(plugin_module)# 假设插件文件中有一个名为 EdgeDetectionPlugin 的类plugin_class = getattr(plugin_module, 'EdgeDetectionPlugin', None)if plugin_class and issubclass(plugin_class, ImageProcessingPlugin):self.current_plugin = plugin_class()QMessageBox.information(self, 'Plugin Loaded', 'Plugin loaded successfully.')else:QMessageBox.warning(self, 'Plugin Error', 'The selected file does not contain a valid ImageProcessingPlugin implementation.')except Exception as e:QMessageBox.warning(self, 'Plugin Error', f'Failed to load plugin: {e}')def detect_edges(self):if not self.file_path:QMessageBox.warning(self, 'No Image Loaded', 'Please load an image first.')returnif not self.current_plugin:QMessageBox.warning(self, 'No Plugin Loaded', 'Please load a plugin first.')returnedges, error = self.current_plugin.process_image(self.file_path)if error:QMessageBox.warning(self, 'Detection Error', error)returnself.image_viewer.display_edges(edges)self.status_label.setText(f'Status: Edges Detected')if __name__ == '__main__':app = QApplication(sys.argv)main_window = MainWindow()main_window.show()sys.exit(app.exec_())

plugin_interface.py

from abc import ABC, abstractmethodclass ImageProcessingPlugin(ABC):@abstractmethoddef process_image(self, image_path):"""处理图像并返回结果"""pass

这篇关于利用python装饰器,客制化图像处理功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093107

相关文章

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚