pyqt+python实现图片裁切
时间: 2023-12-30 10:06:38 浏览: 110
```python
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QVBoxLayout, QWidget, QPushButton, QFileDialog
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import sys
class ImageCropper(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Image Cropper')
self.setGeometry(100, 100, 400, 300)
self.label = QLabel(self)
self.label.setAlignment(Qt.AlignCenter)
self.button = QPushButton('Select Image', self)
self.button.clicked.connect(self.select_image)
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.button)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def select_image(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'Open Image', '', 'Image Files (*.jpg *.png *.jpeg)')
pixmap = QPixmap(file_path)
self.label.setPixmap(pixmap.scaled(300, 300, Qt.KeepAspectRatio))
if __name__ == "__main__":
app = QApplication(sys.argv)
cropper = ImageCropper()
cropper.show()
sys.exit(app.exec_())
```
阅读全文