Python PyQt5 图像查看器:缩放、拖动和点击查看像素坐标
以下是一份使用 Python PyQt5 和 OpenCV 库实现图像查看器的代码,该查看器支持图片缩放、拖动以及点击查看像素坐标的功能。
import cv2
import numpy as np
from PyQt5.QtCore import Qt, QPoint, QRectF
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem
class ImageViewer(QGraphicsView):
def __init__(self, parent=None):
super(ImageViewer, self).__init__(parent)
# 创建场景和图像项
self.scene = QGraphicsScene(self)
self.image_item = QGraphicsPixmapItem()
self.scene.addItem(self.image_item)
self.setScene(self.scene)
# 设置初始状态
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setRenderHint(QPainter.Antialiasing)
self.setRenderHint(QPainter.SmoothPixmapTransform)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
# 连接鼠标事件
self.setMouseTracking(True)
self.mouse_pressed = False
self.last_pos = QPoint(0, 0)
self.zoom_factor = 1.2
self.reset_zoom()
def reset_zoom(self):
# 重置缩放状态
self.zoom_level = 0
self.setTransform(QTransform())
def set_image(self, image):
# 设置图像并自适应大小
self.image = image
qimage = QImage(self.image.data, self.image.shape[1], self.image.shape[0], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
self.image_item.setPixmap(pixmap)
self.fitInView(self.image_item, Qt.KeepAspectRatio)
def mousePressEvent(self, event):
# 处理鼠标按下事件
if event.button() == Qt.LeftButton:
self.mouse_pressed = True
self.last_pos = event.pos()
self.setCursor(Qt.ClosedHandCursor)
def mouseMoveEvent(self, event):
# 处理鼠标移动事件
if self.mouse_pressed:
dx = event.x() - self.last_pos.x()
dy = event.y() - self.last_pos.y()
self.last_pos = event.pos()
self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - dx)
self.verticalScrollBar().setValue(self.verticalScrollBar().value() - dy)
def mouseReleaseEvent(self, event):
# 处理鼠标释放事件
if event.button() == Qt.LeftButton:
self.mouse_pressed = False
self.setCursor(Qt.OpenHandCursor)
def wheelEvent(self, event):
# 处理鼠标滚轮事件
if event.angleDelta().y() > 0:
factor = self.zoom_factor
self.zoom_level += 1
else:
factor = 1 / self.zoom_factor
self.zoom_level -= 1
self.scale(factor, factor)
def mouseDoubleClickEvent(self, event):
# 处理鼠标双击事件
x = event.pos().x()
y = event.pos().y()
scene_pos = self.mapToScene(x, y)
pixmap_rect = self.image_item.boundingRect()
pixmap_pos = self.image_item.pos()
rel_x = (scene_pos.x() - pixmap_pos.x()) / pixmap_rect.width()
rel_y = (scene_pos.y() - pixmap_pos.y()) / pixmap_rect.height()
img_x = int(rel_x * self.image.shape[1])
img_y = int(rel_y * self.image.shape[0])
print('Pixel location: (%d, %d)' % (img_x, img_y))
if __name__ == '__main__':
# 加载图像
image = cv2.imread('test.jpg')
# 创建应用程序和窗口
app = QApplication([])
viewer = ImageViewer()
viewer.set_image(image)
viewer.show()
# 运行应用程序
app.exec_()
代码中主要实现了以下功能:
- 创建一个自定义的 QGraphicsView 控件,用于显示图像。
- 连接鼠标事件,实现拖动、放大缩小和双击查看像素点位置的功能。
- 使用 OpenCV 库加载图像并转换为 QPixmap 格式,显示在 QGraphicsView 中。
- 运行应用程序,显示窗口并进入 Qt 事件循环。
在使用时,只需将代码中的 test.jpg 替换为需要显示的图像路径即可。另外,由于 QGraphicsView 默认会占用鼠标滚轮事件,因此需要在代码中将其禁用,才能实现局部放大缩小的功能。
原文地址: https://www.cveoy.top/t/topic/nv97 著作权归作者所有。请勿转载和采集!