利用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+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Python中4大日志记录库比较的终极PK

《Python中4大日志记录库比较的终极PK》日志记录框架是一种工具,可帮助您标准化应用程序中的日志记录过程,:本文主要介绍Python中4大日志记录库比较的相关资料,文中通过代码介绍的非常详细,... 目录一、logging库1、优点2、缺点二、LogAid库三、Loguru库四、Structlogphp

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

Python海象运算符:=的具体实现

《Python海象运算符:=的具体实现》海象运算符又称​​赋值表达式,Python3.8后可用,其核心设计是在表达式内部完成变量赋值并返回该值,从而简化代码逻辑,下面就来详细的介绍一下如何使用,感兴趣... 目录简介​​条件判断优化循环控制简化​推导式高效计算​正则匹配与数据提取​性能对比简介海象运算符

python项目环境切换的几种实现方式

《python项目环境切换的几种实现方式》本文主要介绍了python项目环境切换的几种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 如何在不同python项目中,安装不同的依赖2. 如何切换到不同项目的工作空间3.创建项目

python项目打包成docker容器镜像的两种方法实现

《python项目打包成docker容器镜像的两种方法实现》本文介绍两种将Python项目打包为Docker镜像的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录简单版:(一次成功,后续下载对应的软件依赖)第一步:肯定是构建dockerfile,如下:第二步

Python + Streamlit项目部署方案超详细教程(非Docker版)

《Python+Streamlit项目部署方案超详细教程(非Docker版)》Streamlit是一款强大的Python框架,专为机器学习及数据可视化打造,:本文主要介绍Python+St... 目录一、针对 Alibaba Cloud linux/Centos 系统的完整部署方案1. 服务器基础配置(阿里