zoukankan      html  css  js  c++  java
  • 压缩图片 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'])
    

      

  • 相关阅读:
    java实训
    二维数组转置
    java第四次上机
    建立一个窗体
    java第三次上机
    数据结构晚自习
    Java程序设计第一次作业
    JAVA程序设计的第一次作业
    java中窗体的转化
    数据结构串的使用
  • 原文地址:https://www.cnblogs.com/shizhengwen/p/7704884.html
Copyright © 2011-2022 走看看