def CompressPng(dir): global changeSize changeFiles = {} if os.path.isdir(dir): imgFiles = GetFileFromThisRootDir(dir, ext) for file in imgFiles: fileName = os.path.basename(file) fileName = fileName.split('.')[0] outPath = "{}/temp_webp/{}.jpg".format(dir, fileName) floder = os.path.dirname(outPath) if not os.path.exists(floder): os.makedirs(floder) originSize = float(os.path.getsize(file)) isChange = changeImage(file, outPath) if isChange < 0: R(outPath) afterSize = float(os.path.getsize(outPath)) if originSize > afterSize: size = originSize - afterSize changeSize = changeSize + size content = "changeFile: {} changeSize: {}".format( file, size) R(content) changeFiles[file] = outPath else: os.remove(outPath) elif os.path.splitext(dir)[1][1:] == 'png': fileName = os.path.basename(file) cmd = "cwebp -q 75 {} -o {}.jpg".format(dir, fileName) os.system(cmd) return changeFiles
def replace(changeFiles): for key in changeFiles: try: imgFloder= os.path.dirname(key) webPFile = changeFiles[key] shutil.copy(webPFile,imgFloder) print key os.remove(key) os.remove(webPFile) except Exception as e: R('出现异常') finally: R("替换下一个文件")
def CompressPng(dir): global changeSize if os.path.isdir(dir): imgFiles = GetFileFromThisRootDir(dir, 'png') for file in imgFiles: originSize = float(os.path.getsize(file)) cmd = "pngquant -f --skip-if-larger --strip --ext=.png --quality 70-70 " + file os.system(cmd) afterSize = float(os.path.getsize(file)) if originSize > afterSize: size = originSize - afterSize changeSize = changeSize + size R("changeFile: " + file +" changeSize:"+str(size)) R(str(size)) elif os.path.splitext(dir)[1][1:] == 'png': cmd = "pngquant -f --skip-if-larger --strip --ext=.png --quality 70-70 " + dir os.system(cmd)
def computerSamilarImg(path, method): global ROOT_PATH global RESULT global METHOD ROOT_PATH = path METHOD = method tranversingDir(path) if len(RESULT) == 0: RESULT = findSamilarImag() R(RESULT) return RESULT
except Exception as e: R('出现异常') finally: R("替换下一个文件") # -------------------------------脚本入口点,以及执行逻辑---------------------------------- if __name__ == '__main__': # 检查是否安装了 pngquant checkCmd = "which cwebp" result = os.system(checkCmd) if result != 0: cmd = "brew install webp" os.system(cmd) compressDir = os.getcwd() # 压缩目录下所有图片 changeFiles=CompressPng(compressDir) B(size_format(changeSize)) str = raw_input('是否替换所有图片:Y/N\n').strip(); if str == "Y": replace(changeFiles) os.removedirs("{}/temp_webp/".format(compressDir)) else: for key in changeFiles: os.remove(changeFiles[key]) os.removedirs("{}/temp_webp".format(compressDir)) R("忽略")
elif os.path.splitext(dir)[1][1:] == 'png': cmd = "pngquant -f --skip-if-larger --strip --ext=.png --quality 70-70 " + dir os.system(cmd) def size_format(b): if b < 1000: return '%i' % b + 'B' elif 1000 <= b < 1000000: return '%.1f' % float(b/1000) + 'KB' elif 1000000 <= b < 1000000000: return '%.1f' % float(b/1000000) + 'MB' elif 1000000000 <= b < 1000000000000: return '%.1f' % float(b/1000000000) + 'GB' elif 1000000000000 <= b: return '%.1f' % float(b/1000000000000) + 'TB' # -------------------------------脚本入口点,以及执行逻辑---------------------------------- if __name__ == '__main__': # 检查是否安装了 pngquant checkCmd = "which pngquant" result = os.system(checkCmd) if result != 0: cmd = "brew install pngquant" os.system(cmd) compressDir = os.getcwd() # 压缩目录下所有图片 CompressPng(compressDir) R(size_format(changeSize))