Last active
October 17, 2020 11:14
-
-
Save drojf/771c19b416752f5b4ddbe3383f618661 to your computer and use it in GitHub Desktop.
Recursively optimize *.png files with ect (ect must already be on command line or adjacent to script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import glob | |
import sys | |
import subprocess | |
from tqdm import tqdm | |
from multiprocessing import Pool | |
# https://github.com/shssoichiro/oxipng | |
def oxipng(path): | |
subprocess.run(['oxipng', '-o', '4', '--strip', 'safe', path]) | |
# https://github.com/fhanau/Efficient-Compression-Tool | |
def ect(path): | |
subprocess.run(['ect', '-9', path]) | |
# Progress bar only works when num_processes = 1 | |
if __name__ == '__main__': | |
# Converting to a list with a huge number of files may be problematic | |
# but it's an easy way to get tqdm to properly display a progress bar | |
paths = list(Path(sys.argv[1]).rglob('**/*.png')) | |
num_processes = 1 | |
if num_processes == 1: | |
for path in tqdm(paths): | |
print(path) | |
ect(path) | |
else: | |
with Pool(num_processes) as p: | |
p.map(ect, paths) | |
print(f"Finished processing {len(paths)} files") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment