pyqt5 graphicsView 加载显示图片,在图片上鼠标框选红色矩形并返回矩形在图片上的坐标
时间: 2023-12-22 15:05:30 浏览: 28
可以通过继承 `QGraphicsView` 和 `QGraphicsScene` 类来实现这个功能。具体实现步骤如下:
1. 创建一个继承自 `QGraphicsView` 的类,并在构造函数中设置 `QGraphicsScene` 对象和一些属性,如下所示:
```python
class ImageViewer(QGraphicsView):
def __init__(self, parent=None):
super(ImageViewer, self).__init__(parent)
self.setScene(QGraphicsScene(self))
self.setRenderHint(QPainter.Antialiasing)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
```
2. 加载图片并显示在 `QGraphicsView` 中,可以使用 `QGraphicsPixmapItem` 对象来实现,如下所示:
```python
def loadImage(self, filename):
pixmap = QPixmap(filename)
self.scene().clear()
self.scene().addPixmap(pixmap)
self.setSceneRect(QRectF(pixmap.rect()))
```
3. 实现鼠标框选功能,可以通过重载 `QGraphicsView` 中的 `mousePressEvent`、`mouseMoveEvent` 和 `mouseReleaseEvent` 方法来实现。具体实现方式如下:
```python
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.origin = event.pos()
self.current_rect = QGraphicsRectItem()
self.current_rect.setPen(QPen(Qt.red, 2))
self.scene().addItem(self.current_rect)
self.current_rect.setPos(self.mapToScene(self.origin))
def mouseMoveEvent(self, event):
if self.origin is not None:
self.current_rect.setRect(QRectF(self.origin, event.pos()).normalized())
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
rect = self.current_rect.rect().normalized()
x, y, w, h = rect.x(), rect.y(), rect.width(), rect.height()
image_rect = self.scene().items()[0].boundingRect()
image_w, image_h = image_rect.width(), image_rect.height()
x_ratio, y_ratio = image_w / self.width(), image_h / self.height()
x, y, w, h = int(x * x_ratio), int(y * y_ratio), int(w * x_ratio), int(h * y_ratio)
print("Rect: ", x, y, w, h)
self.origin = None
```
在 `mousePressEvent` 方法中,我们记录下鼠标起始位置,并创建一个 `QGraphicsRectItem` 对象,设置其样式为红色矩形,并将其添加到场景中。在 `mouseMoveEvent` 方法中,我们实时更新矩形的位置和大小。在 `mouseReleaseEvent` 方法中,我们计算出矩形在图片中的坐标,并输出矩形的位置和大小。
完整的代码示例:
```python
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
class ImageViewer(QGraphicsView):
def __init__(self, parent=None):
super(ImageViewer, self).__init__(parent)
self.setScene(QGraphicsScene(self))
self.setRenderHint(QPainter.Antialiasing)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.origin = None
self.current_rect = None
def loadImage(self, filename):
pixmap = QPixmap(filename)
self.scene().clear()
self.scene().addPixmap(pixmap)
self.setSceneRect(QRectF(pixmap.rect()))
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.origin = event.pos()
self.current_rect = QGraphicsRectItem()
self.current_rect.setPen(QPen(Qt.red, 2))
self.scene().addItem(self.current_rect)
self.current_rect.setPos(self.mapToScene(self.origin))
def mouseMoveEvent(self, event):
if self.origin is not None:
self.current_rect.setRect(QRectF(self.origin, event.pos()).normalized())
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
rect = self.current_rect.rect().normalized()
x, y, w, h = rect.x(), rect.y(), rect.width(), rect.height()
image_rect = self.scene().items()[0].boundingRect()
image_w, image_h = image_rect.width(), image_rect.height()
x_ratio, y_ratio = image_w / self.width(), image_h / self.height()
x, y, w, h = int(x * x_ratio), int(y * y_ratio), int(w * x_ratio), int(h * y_ratio)
print("Rect: ", x, y, w, h)
self.origin = None
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
viewer = ImageViewer()
viewer.loadImage("test.png")
viewer.show()
sys.exit(app.exec_())
```
其中,`test.png` 是需要显示的图片文件路径。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)