利用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 FastAPI实现JWT校验的完整指南

《PythonFastAPI实现JWT校验的完整指南》在现代Web开发中,构建安全的API接口是开发者必须面对的核心挑战之一,本文将深入探讨如何基于FastAPI实现JWT(JSONWebToken... 目录一、JWT认证的核心原理二、项目初始化与环境配置三、安全密码处理机制四、JWT令牌的生成与验证五、

Python使用Turtle实现精确计时工具

《Python使用Turtle实现精确计时工具》这篇文章主要为大家详细介绍了Python如何使用Turtle实现精确计时工具,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录功能特点使用方法程序架构设计代码详解窗口和画笔创建时间和状态显示更新计时器控制逻辑计时器重置功能事件

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Python模拟串口通信的示例详解

《Python模拟串口通信的示例详解》pySerial是Python中用于操作串口的第三方模块,它支持Windows、Linux、OSX、BSD等多个平台,下面我们就来看看Python如何使用pySe... 目录1.win 下载虚www.chinasem.cn拟串口2、确定串口号3、配置串口4、串口通信示例5

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失

利用Python实现Excel文件智能合并工具

《利用Python实现Excel文件智能合并工具》有时候,我们需要将多个Excel文件按照特定顺序合并成一个文件,这样可以更方便地进行后续的数据处理和分析,下面我们看看如何使用Python实现Exce... 目录运行结果为什么需要这个工具技术实现工具的核心功能代码解析使用示例工具优化与扩展有时候,我们需要将

Python+PyQt5实现文件夹结构映射工具

《Python+PyQt5实现文件夹结构映射工具》在日常工作中,我们经常需要对文件夹结构进行复制和备份,本文将带来一款基于PyQt5开发的文件夹结构映射工具,感兴趣的小伙伴可以跟随小编一起学习一下... 目录概述功能亮点展示效果软件使用步骤代码解析1. 主窗口设计(FolderCopyApp)2. 拖拽路径

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件