pyqt5 graphicsView 加载显示图片,在图片上鼠标框选红色矩形并返回矩形在图片上的坐标
时间: 2023-12-22 10:05:56 浏览: 29
可以通过以下步骤实现:
1. 创建一个 GraphicsView 对象和一个 QGraphicsScene 对象。
```python
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene
view = QGraphicsView()
scene = QGraphicsScene()
view.setScene(scene)
```
2. 在 QGraphicsScene 中添加图片。
```python
from PyQt5.QtGui import QPixmap
pixmap = QPixmap("path/to/image.jpg")
scene.addPixmap(pixmap)
```
3. 创建一个 QGraphicsRectItem 对象,用于表示红色矩形。
```python
from PyQt5.QtGui import QPen, QColor
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtWidgets import QGraphicsRectItem
pen = QPen(QColor(Qt.red))
pen.setWidth(2)
rect_item = QGraphicsRectItem()
rect_item.setPen(pen)
```
4. 在 GraphicsView 中重载 mousePressEvent 和 mouseReleaseEvent 方法,分别记录鼠标点击和释放的位置。
```python
class MyGraphicsView(QGraphicsView):
def __init__(self):
super().__init__()
def mousePressEvent(self, event):
self.start_pos = event.pos()
def mouseReleaseEvent(self, event):
end_pos = event.pos()
self.draw_rect(self.start_pos, end_pos)
def draw_rect(self, start_pos, end_pos):
# 计算矩形的位置和大小
rect = QRectF(start_pos, end_pos)
rect = rect.normalized()
# 设置矩形的位置和大小
rect_item.setRect(rect)
# 将矩形添加到场景中
scene.addItem(rect_item)
# 计算矩形在图片上的位置
rect_in_image = view.mapToScene(rect_item.boundingRect()).boundingRect()
print(rect_in_image)
```
5. 在 draw_rect 方法中,计算矩形在图片上的位置,并输出结果。
完整代码如下:
```python
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene
from PyQt5.QtGui import QPixmap, QPen, QColor
from PyQt5.QtCore import Qt, QRectF
view = QGraphicsView()
scene = QGraphicsScene()
view.setScene(scene)
pixmap = QPixmap("path/to/image.jpg")
scene.addPixmap(pixmap)
pen = QPen(QColor(Qt.red))
pen.setWidth(2)
rect_item = QGraphicsRectItem()
rect_item.setPen(pen)
class MyGraphicsView(QGraphicsView):
def __init__(self):
super().__init__()
def mousePressEvent(self, event):
self.start_pos = event.pos()
def mouseReleaseEvent(self, event):
end_pos = event.pos()
self.draw_rect(self.start_pos, end_pos)
def draw_rect(self, start_pos, end_pos):
rect = QRectF(start_pos, end_pos)
rect = rect.normalized()
rect_item.setRect(rect)
scene.addItem(rect_item)
rect_in_image = view.mapToScene(rect_item.boundingRect()).boundingRect()
print(rect_in_image)
view = MyGraphicsView()
view.show()
```
相关推荐
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)