美文网首页
tiny批量压缩图片

tiny批量压缩图片

作者: 激扬飞雪 | 来源:发表于2019-06-25 11:57 被阅读0次

tiny

介绍

tiny批量压缩图片

源代码

https://gitee.com/zhoulikai/tiny

import os
import sys
import tinify
'''
    列出所有path及其子目录下符合fileter条件的文件
'''
def list_dir(path, res, filter):
    if path:
        for file in os.listdir(path):
            temp_dir = os.path.join(path, file)
            if os.path.isdir(temp_dir):
                list_dir(temp_dir, res, filter)
            else:
                if (file.endswith(filter) and not file.startswith(".")):
                    res.append(temp_dir)
        return res
    else:
        return res

'''
    使用find 命令搜索所有path及其子目录下符合fileter条件的文件
'''
def list_dir_by_shell(path, filter):
    find_file_shell = 'find ' + path + ' -type f -name ' + '*' + filter
    # print(find_file_shell)
    output = os.popen(find_file_shell)
    result = output.read()
    # print(result)
    return result.split()

'''
    tiny 压缩
'''
def tiny(path, filter):
    tinify.key = "J3ufeOH47y0x5NJVvdbBJY3DkhI7EzWZ"
    res = []
    list_dir(path, res, filter)
    # res = list_dir_by_shell(path, filter)
    if res:
        tiny_dir_name = "tiny_" + os.path.basename(path) ;
        tiny_dir_path = os.path.join(os.path.dirname(path), tiny_dir_name)
        if not os.path.exists(tiny_dir_path):
            os.makedirs(tiny_dir_path)
        for file in res:
            print("压缩开始" + file)
            source = tinify.from_file(file)
            tiny_file = os.path.join(tiny_dir_path, os.path.basename(file))
            source.to_file(tiny_file)
            print(file + "tiny 压缩成功:压缩后的文件为:" + tiny_file)
    else:
        print("没有要压缩的文件")
    

def main():
    if len(sys.argv) <= 2:
        print("请输入要压缩的目录及过滤条件")
        return 
    path = sys.argv[1]
    filter = sys.argv[2]
    tiny(path, filter)

if __name__ == "__main__":
    main()

运行环境

  1. 操作系统: Linux or Mac
  2. python版本: python3以上
  3. 依赖: pip install --upgrade tinify

使用说明

  1. python tiny.py [要压缩的文件所在的目录] [搜索的条件如只压缩png或者jpg]
  2. 例如: python tiny.py /var/www/resume/img/ott jpg
  3. 压缩后的文件所在目录:/var/www/resume/img/tiny_ott

相关文章

  • tiny批量压缩图片

    tiny 介绍 tiny批量压缩图片 源代码 https://gitee.com/zhoulikai/tiny 运...

  • 在线压缩图片-tinypng网站

    ●如果一款app打开之后,页面缓冲很卡,图片加载很慢,那么建议使用tiny网站,在线批量压缩图片。 ●如果你发送邮...

  • 网址

    苹果官方swift社区 git教程 sketch中文网 Tiny:图片压缩

  • apk压缩记录

    一、图片压缩 打开 Tiny压缩 直接上传图片并下载下来就已经进行压缩了,简单方便; 二、7Z压缩上线前 将APK...

  • Webp图片批处理程序

    功能 1、批量将PNG/JPEG格式图片转换Webp格式图片2、批量将Webp格式图片再压缩【压缩效率0-100可...

  • pngquant图片压缩

    pngquant pngquant可以通过命令行压缩png图片。 脚本批量压缩png图片 quality表示压缩的...

  • iOS优化-瘦身

    图片 1、批量压缩图片(tinyPNG) 用TinyPNG[https://tinypng.com/]将图片进行压...

  • webpack-imgmini,一个运用webpack批量压缩图

    运用webpack批量压缩图片的小工具,支持png,jpg,gif,对大图片压缩效果较好 项目地址:https:/...

  • iOS 优化

    资源优化:对资源文件下手,压缩图片/音频,去除不必要的资源iOS项目瘦身,删除无用图片,批量压缩图片 编译优化:r...

  • 项目图片分析和自动压缩脚本(python)

    1) 脚本的功能 对项目中的图片(某一个目录的图片)使用tinify 进行批量压缩, 压缩完毕之后存到当前的脚本目...

网友评论

      本文标题:tiny批量压缩图片

      本文链接:https://www.haomeiwen.com/subject/jzhtcctx.html