-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageviewer.py
63 lines (52 loc) · 1.98 KB
/
imageviewer.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
from PySide2.QtCore import QSize
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QLabel
class ImageViewer(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.factor = 1.0
self.setScaledContents(True)
def set_image(self, path='', ):
# check file exists
pixmap = QPixmap()
pixmap.load(path)
# check null
self.setPixmap(pixmap)
self.adjustSize()
return pixmap
def scale_image_by_rate(self, rate):
if self.factor * rate > 4.76:
self.factor = 4.76
elif self.factor * rate < 0.1:
self.factor = 0.1
else:
self.factor = round(rate * self.factor, 2)
self.scale_image_by_factor(self.factor)
print(self.factor)
def scale_image_by_factor(self, factor):
if factor > 4.76:
factor = 4.76
elif factor < 0.1:
factor = 0.1
self.factor = factor
self.resize(self.factor * self.pixmap().size())
def resize_keep_aspect_ratio(self, base_size=500):
height = self.pixmap().height()
width = self.pixmap().width()
if height > width:
self.resize(base_size, base_size*self.aspect_ratio())
else:
self.resize(base_size*self.aspect_ratio(), base_size)
def aspect_fit(self, size: QSize):
win_gradient = size.height() / size.width()
pix_gradient = self.pixmap().height() / self.pixmap().width()
if win_gradient > pix_gradient:
self.resize(size.width(), int(size.width() * pix_gradient))
self.factor = round(size.width() / self.pixmap().width(), 2)
else:
self.resize(int(size.height() / pix_gradient), size.height())
self.factor = round(size.height() / self.pixmap().height(), 2)
def aspect_ratio(self):
width = self.pixmap().width()
height = self.pixmap().height()
return max(width, height) / min(width, height)