zoukankan      html  css  js  c++  java
  • 批量对图片进行缩放

    1.直接上代码,就是对某文件夹下的图片进行批量缩放

    #!python
    import os,sys
    import plistlib
    from PIL import Image
    
    scale_param = 0.852
    root_save_path = 'c:/scale'
    
    def scale_png(path, file, save_path):
        png_path = path + '/' + file
        big_image = Image.open(png_path)
        img_size = big_image.size
        scale_size = [img_size[0] * scale_param, img_size[1] * scale_param]
        # print(scale_param, img_size, scale_size)
        big_image.thumbnail(scale_size, Image.ANTIALIAS)
        big_image.save(save_path + '/' + file)
            
        
    def list_file(path, save_path):
        for file in os.listdir(path):
            if os.path.isdir(path + '/' + file):
                save_path = save_path + '/' + file
                if not os.path.exists(save_path) :
                    os.mkdir(save_path)
                list_file(path + '/' + file, save_path)
                continue
            file_names = os.path.splitext(file)
            print file_names
            if file_names[1] == '.png' or file_names[1] == '.jpg' :
                scale_png(path, file, save_path )
    
    if __name__ == '__main__':
        path = sys.argv[1]
        root_save_path = sys.argv[2]
        scale_param = float(sys.argv[3])
        if not os.path.exists(root_save_path):
            os.mkdir(root_save_path)
    
        if os.path.exists(path) :
            list_file( path, root_save_path )
        else :
            print('please input correct path')
    
    
  • 相关阅读:
    20170416
    汇总02
    总结
    在编程的世界中,如何高效地学习理论知识,应用理论知识来解决实际生产中的问题
    周末待整理
    web 性能提升
    es6
    http、https、 json、 ajax
    微信小程序 问题收集
    eslint
  • 原文地址:https://www.cnblogs.com/zjzyh/p/7421453.html
Copyright © 2011-2022 走看看