压缩图片 Image
图片压缩
class resizeImg: """缩略图""" def __init__(self,**args): self.args_key = {'ori_img': '', 'dst_img': '', 'dst_h': '','dst_w': ''} self.arg = {} for key in self.args_key: if key in args: self.arg[key] = args[key] self.im = Image.open(self.arg['ori_img']) self.ori_w, self.ori_h = self.im.size self.heightRatio = None self.widthRatio = None self.ratio = 1 def hight(self): if self.ori_h and self.ori_h > self.arg['dst_h']: if self.arg['dst_h'] and self.ori_h > self.arg['dst_h']: self.heightRatio = float(self.arg['dst_h']) / self.ori_h ratio = self.heightRatio newWidth = int(self.ori_w * ratio) newHeight = int(self.ori_h * ratio) else: newWidth = self.ori_w newHeight = self.ori_h self.im.resize((newWidth, newHeight)).save(self.arg['dst_img']) def width(self): if self.ori_w and self.ori_w > self.arg['dst_w']: if self.arg['dst_w'] and self.ori_w > self.arg['dst_w']: self.widthRatio = float(self.arg['dst_w']) / self.ori_w ratio = self.widthRatio newWidth = int(self.ori_w * ratio) newHeight = int(self.ori_h * ratio) else: newWidth = self.ori_w newHeight = self.ori_h self.im.resize((newWidth, newHeight)).save(self.arg['dst_img'])