-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ImageRotate.py
114 lines (98 loc) · 4.01 KB
/
ImageRotate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年11月19日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file:
@description:
"""
try:
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QImage
from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, \
QVBoxLayout, QHBoxLayout, QSpacerItem, QSizePolicy, QApplication
except ImportError:
from PySide2.QtCore import Qt
from PySide2.QtGui import QPixmap, QPainter, QImage
from PySide2.QtWidgets import QWidget, QLabel, QPushButton, \
QVBoxLayout, QHBoxLayout, QSpacerItem, QSizePolicy, QApplication
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.imageLabel = QLabel(self)
self.imageLabel.setAlignment(Qt.AlignCenter)
layout.addWidget(self.imageLabel)
clayout = QHBoxLayout()
layout.addItem(clayout)
clayout.addItem(QSpacerItem(
40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
clayout.addWidget(QPushButton('水平翻转', self, clicked=self.doHorFilp))
clayout.addWidget(QPushButton('垂直翻转', self, clicked=self.doVerFilp))
clayout.addWidget(QPushButton(
'顺时针45度', self, clicked=self.doClockwise))
clayout.addWidget(QPushButton(
'逆时针45度', self, clicked=self.doAnticlockwise))
clayout.addItem(QSpacerItem(
40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
# 原始图片
self.srcImage = QImage('Data/fg.png')
self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
def doHorFilp(self):
# 水平翻转
self.srcImage = self.srcImage.mirrored(True, False)
self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
def doVerFilp(self):
# 垂直翻转
self.srcImage = self.srcImage.mirrored(False, True)
self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
def doClockwise(self):
# 顺时针45度
image = QImage(self.srcImage.size(),
QImage.Format_ARGB32_Premultiplied)
painter = QPainter()
painter.begin(image)
# 以图片中心为原点
hw = self.srcImage.width() / 2
hh = self.srcImage.height() / 2
painter.translate(hw, hh)
painter.rotate(45) # 旋转45度
painter.drawImage(-hw, -hh, self.srcImage) # 把图片绘制上去
painter.end()
self.srcImage = image # 替换
self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
# # 下面这个旋转方法针对90度的倍数,否则图片会变大
# trans = QTransform()
# trans.rotate(90)
# self.srcImage = self.srcImage.transformed(
# trans, Qt.SmoothTransformation)
# self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
def doAnticlockwise(self):
# 逆时针45度
image = QImage(self.srcImage.size(),
QImage.Format_ARGB32_Premultiplied)
painter = QPainter()
painter.begin(image)
# 以图片中心为原点
hw = self.srcImage.width() / 2
hh = self.srcImage.height() / 2
painter.translate(hw, hh)
painter.rotate(-45) # 旋转-45度
painter.drawImage(-hw, -hh, self.srcImage) # 把图片绘制上去
painter.end()
self.srcImage = image # 替换
self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
# # 下面这个旋转方法针对90度的倍数,否则图片会变大
# trans = QTransform()
# trans.rotate(90)
# self.srcImage = self.srcImage.transformed(
# trans, Qt.SmoothTransformation)
# self.imageLabel.setPixmap(QPixmap.fromImage(self.srcImage))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())