ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python版使用tinypng压缩图片大小

2021-09-08 12:04:12  阅读:190  来源: 互联网

标签:python 压缩 tinypng print path 图片大小 os eachFile size


第一步,前往tinypng官网或组自己的key值。

第二步,配置uncompressResPath、compressedResPath、largeRes路径。

第三步,拷贝以下代码执行后就能得到压缩后的图片。

tinypng的压缩效果非常好,但是免费用户每个月最多只能上传压缩500张图片,想要压缩更多的图片可以赞助一下tinypng,或者注册多个账号。

tinypng压缩效果的确非常好,虽然是有损压缩,但是压缩质量很高,在不进行大幅度缩放的情况下肉眼基本上看不出来跟原图的差异,压缩比基本上在60%以上(个人使用总结)。

压缩速度会受到网速的影响,所以有时可能会很久才压缩完一张。

tinypng官网上还支持拖拽图片上传压缩的方式,有一些限制,具体可以查看官网。

#coding=utf-8
import tinify
import os
import shutil
tinify.key = "填写自己的key值"
# tinify.proxy = "http://user:pass@192.168.0.1:8080"
#未压缩图片路径
uncompressResPath = "未压缩图片的图片根路径";
#压缩的图片存放路径
compressedResPath = "压缩完成的图片存放路径";
#用于存放检索出的符合条件的大图
largeRes =  "存放检索出的符合条件的大图";

#每个月能够使用的最大压缩数量

maxMonthCompressCount = 500;
compressions_this_month = 0;
BITAXSIZE = 1024.0
KBMAXSIZE = BITAXSIZE * 1024.0
MBMAXSIZE = KBMAXSIZE * 1024.0
GBMAXSIZE = MBMAXSIZE * 1024.0
TBMAXSIZE = GBMAXSIZE * 1024.0
totalSize = 0;
#压缩单张资源
def compressSimpleRes(resAbsolutePath,fileName):
    global compressions_this_month;
    print("start compress res");
    print("original Res Size:" + size_format(os.path.getsize(resAbsolutePath)));
    source = tinify.from_file(resAbsolutePath);
    print("upload Res success");
    source.to_file(compressedResPath +fileName);
    compressions_this_month = tinify.compression_count;
    print("left Compress Times:" + "%i"%(maxMonthCompressCount - compressions_this_month));
    print("compress res success");
    print("compressed Res Size:" + size_format(os.path.getsize(compressedResPath +fileName)))
#压缩根目录下指定数量的png资源
def compressAllPng(path):
    allFile = os.listdir(path);
    for eachFile in allFile:
        if os.path.isdir(path + os.sep + eachFile):
            compressAllPng(path + os.sep + eachFile);
        else:
            if(".png" in eachFile):
                if(compressions_this_month >= maxMonthCompressCount):
                    return
                print("res Name:" + eachFile);
                compressSimpleRes(path + os.sep + eachFile,eachFile);
#获得文件大小
def size_format(size):
    if size < BITAXSIZE:
        return '%i' % size + 'size'
    elif BITAXSIZE <= size < KBMAXSIZE:
        return '%.2f' % round(size/BITAXSIZE,2)+'KB'
    elif KBMAXSIZE <= size < MBMAXSIZE:
        return '%.2f' % round(size/KBMAXSIZE,2)+'MB'
    elif MBMAXSIZE <= size < GBMAXSIZE:
        return '%.2f' % round(size/MBMAXSIZE,2)+'GB'
    elif GBMAXSIZE <= size:
        return '%.2f' % round(size/GBMAXSIZE,2)+'TB'
#拷贝文件到目标目录
def copyToDest(filePath):
    shutil.copy(filePath,largeRes + os.sep);
#收集根目录下大于指定大小的png资源
def collectLargeSizeRes(path):
    global totalSize;
    allFile = os.listdir(path);
    for eachFile in allFile:
        fileAbsolutePath = path + os.sep + eachFile;
        if os.path.isdir(fileAbsolutePath):
            collectLargeSizeRes(fileAbsolutePath);
        else:
            if(".png" in eachFile):
                fileSize = os.path.getsize(fileAbsolutePath);

                #如果文件大于1M则将此文件copy到largeRes文件夹
                if(fileSize >= KBMAXSIZE):
                    totalSize = totalSize + fileSize;
                    print("total size:" + size_format(totalSize));
                    copyToDest(fileAbsolutePath);
collectLargeSizeRes(uncompressResPath);
compressAllPng(largeRes);
# print(compressions_this_month);

标签:python,压缩,tinypng,print,path,图片大小,os,eachFile,size
来源: https://blog.csdn.net/min187306/article/details/120176754

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有