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)
  • 相关阅读:
    2016-7-4工作总结
    2016-7第一周工作总结
    2016-6-30 工作总结
    2016-6-29 工作总结
    2016-6-28 工作总结
    基于软件开发对嵌入式开发的思考
    团队项目总结
    软件工程课程总结
    图描述之:流程图
    004-二叉树的遍历
  • 原文地址:https://www.cnblogs.com/dangzhengtao/p/9620272.html
Copyright © 2011-2022 走看看