zoukankan      html  css  js  c++  java
  • Python3.7 练习题(三) 将指定目录下的图片进行批量尺寸大小处理

    # 将指定目录下的图片进行批量尺寸大小处理
    
    #修改图片尺寸  导入Image os 快捷键 alt+enter
    import os
    from PIL import Image
    
    
    def process_image(filename,width = 640,hight = 1136):
        image = Image.open(filename)
        image_width = image.width
        image_height = image.height
        if image_width <= width and image_height <= hight:
            print(filename," is ok")
            return
        if 1.0*image_width/width > 1.0*image_height/hight:
            scale = 1.0 * image_width/width
            new_image = image.resize((int(image_width/scale),int(image_height/scale)),Image.ANTIALIAS)
        else:
            scale = 1.0 * image_height/hight
            new_image = image.resize((int(image_width / scale), int(image_height / scale)), Image.ANTIALIAS)
    
        new_image.save("new--"+filename)
        new_image.close()
    
    
    #获取目录下面的文件的后缀 ext
    = ['jpg','png','jpeg'] files = os.listdir('.') for file in files: if file.split('.')[-1] in ext: process_image(file)
  • 相关阅读:
    7
    6
    5
    3
    4
    2
    1
    寒假工作经历
    软件工程第三周的总结
    软件工程第三周的学习报告 html<input> final finally finalize 的比较 BigInteger
  • 原文地址:https://www.cnblogs.com/dangzhengtao/p/9620272.html
Copyright © 2011-2022 走看看