Qt Widget 窗口中旋转图像 90 度
可以使用 QPixmap 和 QTransform 来实现图像旋转 90 度。以下是一个示例代码:
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QTransform
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Image Rotation')
# 加载图像
pixmap = QPixmap('image.jpg')
# 旋转图像
transform = QTransform().rotate(90)
rotated_pixmap = pixmap.transformed(transform, Qt.SmoothTransformation)
# 显示旋转后的图像
label = QLabel(self)
label.setPixmap(rotated_pixmap)
label.setGeometry(0, 0, 400, 400)
if __name__ == '__main__':
app = QApplication([])
widget = MyWidget()
widget.show()
app.exec_()
在这个例子中,我们加载了一个名为 'image.jpg' 的图像,并将其旋转了 90 度。然后,我们使用 QLabel 将旋转后的图像显示在窗口中。
原文地址: https://www.cveoy.top/t/topic/nk6L 著作权归作者所有。请勿转载和采集!