PyQt5实现图片裁剪
时间: 2023-09-21 08:10:47 浏览: 484
Python实现网页截图(PyQT5)过程解析
可以使用QPixmap和QGraphicsScene来实现图片裁剪。
首先,需要在PyQt5中导入以下模块:
```python
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QFileDialog, QGraphicsRectItem, QGraphicsItem
from PyQt5.QtGui import QPixmap, QCursor
from PyQt5.QtCore import Qt, QRectF
```
接着,创建一个QGraphicsScene对象并将其设置为QGraphicsView的场景:
```python
scene = QGraphicsScene()
view = QGraphicsView(scene)
```
然后,使用QFileDialog打开一张图片,将其加载到QPixmap对象中,并将该对象设置为QGraphicsPixmapItem的Pixmap:
```python
filename, _ = QFileDialog.getOpenFileName(None, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
if filename:
pixmap = QPixmap(filename)
pixmap_item = QGraphicsPixmapItem(pixmap)
scene.addItem(pixmap_item)
```
接下来,使用QGraphicsRectItem创建一个裁剪框,并将其添加到场景中:
```python
rect_item = QGraphicsRectItem()
rect_item.setPen(Qt.red)
rect_item.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
rect_item.setZValue(1)
scene.addItem(rect_item)
```
为了使裁剪框可以拖动和缩放,需要在鼠标按下事件和鼠标移动事件中添加代码:
```python
def mousePressEvent(event):
if event.button() == Qt.LeftButton:
rect_item.setFlags(QGraphicsItem.ItemIsMovable)
rect_item.setCursor(QCursor(Qt.ClosedHandCursor))
else:
rect_item.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
rect_item.setCursor(QCursor(Qt.ArrowCursor))
def mouseMoveEvent(event):
if rect_item.flags() & QGraphicsItem.ItemIsMovable:
rect_item.setCursor(QCursor(Qt.ClosedHandCursor))
rect = QRectF(event.scenePos(), rect_item.rect().size())
if rect.intersects(pixmap_item.boundingRect()):
rect_item.setRect(rect)
```
最后,添加一个按钮,当用户点击该按钮时,将裁剪后的图片保存到本地:
```python
def save_image():
rect = rect_item.rect()
cropped_pixmap = pixmap.copy(rect.toRect())
cropped_pixmap.save("cropped_image.png", "PNG")
button = QPushButton("Save Image")
button.clicked.connect(save_image)
```
完整代码如下所示:
```python
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QFileDialog, QGraphicsRectItem, QGraphicsItem, QPushButton
from PyQt5.QtGui import QPixmap, QCursor
from PyQt5.QtCore import Qt, QRectF
app = QApplication([])
scene = QGraphicsScene()
view = QGraphicsView(scene)
filename, _ = QFileDialog.getOpenFileName(None, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
if filename:
pixmap = QPixmap(filename)
pixmap_item = QGraphicsPixmapItem(pixmap)
scene.addItem(pixmap_item)
rect_item = QGraphicsRectItem()
rect_item.setPen(Qt.red)
rect_item.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
rect_item.setZValue(1)
scene.addItem(rect_item)
def mousePressEvent(event):
if event.button() == Qt.LeftButton:
rect_item.setFlags(QGraphicsItem.ItemIsMovable)
rect_item.setCursor(QCursor(Qt.ClosedHandCursor))
else:
rect_item.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
rect_item.setCursor(QCursor(Qt.ArrowCursor))
def mouseMoveEvent(event):
if rect_item.flags() & QGraphicsItem.ItemIsMovable:
rect_item.setCursor(QCursor(Qt.ClosedHandCursor))
rect = QRectF(event.scenePos(), rect_item.rect().size())
if rect.intersects(pixmap_item.boundingRect()):
rect_item.setRect(rect)
view.mousePressEvent = mousePressEvent
view.mouseMoveEvent = mouseMoveEvent
def save_image():
rect = rect_item.rect()
cropped_pixmap = pixmap.copy(rect.toRect())
cropped_pixmap.save("cropped_image.png", "PNG")
button = QPushButton("Save Image")
button.clicked.connect(save_image)
view.show()
button.show()
app.exec_()
```
阅读全文