基本实现图片缩小和放大 版本1
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QSize,Qt
from PIL import Image
def shrinkImage_down(self):
'''
缩小图片
:return:
'''
scale = 0.8
img = QImage(self.path)
newWidth = int(Width * scale)
newHeight = int(Height * scale)
size = QSize(newWidth , newHeight )
pixImg = QPixmap.fromImage(img.scaled(size, Qt.IgnoreAspectRatio))
self.imageLabel.resize(newWidth , newHeight )
self.imageLabel.setPixmap(pixImg)
def shrinkImage_up(self):
'''
缩小图片
:return:
'''
scale = 1.2
img = QImage(self.path)
newWidth = int(Width * scale)
newHeight = int(Height * scale)
size = QSize(newWidth , newHeight )
pixImg = QPixmap.fromImage(img.scaled(size, Qt.IgnoreAspectRatio))
self.imageLabel.resize(newWidth , newHeight)
self.imageLabel.setPixmap(pixImg)
基本实现图片缩小和放大 版本2
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QSize,Qt
from PIL import Image
def shrinkImage(label_obj, base_scale=1, base_scale_min=1, base_scale_max=8, image_path='', rate=1):
"""
调整图片大小,放大和缩小,放大最大8倍,缩小最小1倍
:param label_obj: 传入的pyQt中 QLabel 对象
:param base_scale: 最初定义倍率
:param base_scale_min: 最小倍率 1
:param base_scale_max: 最大倍率 8
:param image_path: 被调整的图片
:param rate: 倍率(1,2,4,8)
:return:
"""
scale = base_scale * rate
base_scale = scale
if base_scale < base_scale_min:
scale = base_scale_min
base_scale = base_scale_min
elif base_scale > base_scale_max:
scale = base_scale_max
base_scale = base_scale_max
img = QImage(image_path)
mgnWidth = int(img.size().width() * scale)
mgnHeight = int(img.size().height() * scale)
size = QSize(mgnWidth, mgnHeight)
pixImg = QPixmap.fromImage(img.scaled(size, Qt.IgnoreAspectRatio))
label_obj.resize(mgnWidth, mgnHeight)
label_obj.setPixmap(pixImg)
return base_scale