1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| import os import shutil
def is_img(ext): ext = ext.lower() if ext in ['.jpg', '.png', '.jpeg', '.bmp']: return True else: return False
TargetPath = 'Your Target Path' SourcePath = 'Your Source Path'
def get_img_files(dir): py_files = [] for root, dirs, files in os.walk(dir): for file in files: pre, suf = os.path.splitext(file) if is_img(suf): py_files.append(os.path.join(root, file)) return py_files
TargetFiles = get_img_files(TargetPath) SourceFiles = get_img_files(SourcePath)
for target in TargetFiles: for source in SourceFiles: targetName = target.split('/')[-1] sourceName = source.split('/')[-1] if targetName == sourceName: shutil.copyfile(source, target) 图片批量压缩 & 替换,二合一 这样通过两个脚本就可以实现批量压缩、替换,but,我要跑两个脚本,好麻烦,能不能合二为一,就问你能不能? 小样,这怎么可能难倒机智的我,
压缩脚本的输出目录是替换脚本的源目录,压缩脚本的源目录是替换脚本的输出目录 所以改一下压缩脚本的实现,读取目录改成是固定的,再改一下压缩脚本的输出目录,注意跟读取目录不要再是读取的子目录,要不然会有问题 然后在压缩脚本执行成功后,执行批量替换脚本,done
import os import sys import os.path import click import tinify import shutil
tinify.key = "Your API KEY" version = "1.0.1"
TargetPath = 'Your Target Path' SourcePath = 'Your Source Path'
def compress_core(inputFile, outputFile, img_width): source = tinify.from_file(inputFile) if img_width is not -1: resized = source.resize(method = "scale", width = img_width) resized.to_file(outputFile) else: source.to_file(outputFile)
def compress_path(path, width): print ("compress_path-------------------------------------") if not os.path.isdir(path): print ("这不是一个文件夹,请输入文件夹的正确路径!") return else: fromFilePath = path toFilePath = SourcePath print ("fromFilePath=%s" %fromFilePath) print ("toFilePath=%s" %toFilePath)
for root, dirs, files in os.walk(fromFilePath): print ("root = %s" %root) print ("dirs = %s" %dirs) print ("files= %s" %files) for name in files: fileName, fileSuffix = os.path.splitext(name) if fileSuffix == '.png' or fileSuffix == '.jpg' or fileSuffix == '.jpeg': toFullPath = toFilePath + root[len(fromFilePath):] toFullName = toFullPath + '/' + name if os.path.isdir(toFullPath): pass else: os.mkdir(toFullPath) compress_core(root + '/' + name, toFullName, width)
def compress_file(inputFile, width): print ("compress_file-------------------------------------") if not os.path.isfile(inputFile): print ("这不是一个文件,请输入文件的正确路径!") return print ("file = %s" %inputFile) dirname = os.path.dirname(inputFile) basename = os.path.basename(inputFile) fileName, fileSuffix = os.path.splitext(basename) if fileSuffix == '.png' or fileSuffix == '.jpg' or fileSuffix == '.jpeg': compress_core(inputFile, dirname+"/tiny_"+basename, width) else: print ("不支持该文件类型!")
def is_img(ext): ext = ext.lower() if ext in ['.jpg', '.png', '.jpeg', '.bmp']: return True else: return False
def get_img_files(dir): py_files = [] for root, dirs, files in os.walk(dir): for file in files: pre, suf = os.path.splitext(file) if is_img(suf): py_files.append(os.path.join(root, file)) return py_files
def batch_replace_img(): TargetFiles = get_img_files(TargetPath) SourceFiles = get_img_files(SourcePath)
for target in TargetFiles: for source in SourceFiles: targetName = target.split('/')[-1] sourceName = source.split('/')[-1] if targetName == sourceName: shutil.copyfile(source, target)
@click.command() @click.option('-f', "--file", type=str, default=None, help="单个文件压缩") @click.option('-d', "--dir", type=str, default=None, help="被压缩的文件夹") @click.option('-w', "--width", type=int, default=-1, help="图片宽度,默认不变") def run(file, dir, width): print ("GcsSloop TinyPng V%s" %(version)) if file is not None: compress_file(file, width) pass elif dir is not None: compress_path(dir, width) pass else: compress_path(os.getcwd(), width) print ("压缩结束!")
if __name__ == "__main__": compress_path(TargetPath, -1) batch_replace_img() print("替换结束")
|