This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makejxl.py
86 lines (72 loc) · 2.73 KB
/
makejxl.py
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
#!/usr/bin/env python3
# Based on https://geoffruddock.com/bulk-makejxl-x265-with-ffmpeg/
import os
import pathlib
import sys
from shutil import which
from subprocess import run
from typing import Iterable
import click
__version__ = '0.1.5'
SUPPORTED_FORMATS = ['jpeg', 'jpg', 'png', 'apng', 'gif', 'exr', 'ppm', 'pfm', 'pgx']
# Ported from: https://github.com/victordomingos/optimize-images
def search_files(dirpath: str, recursive: bool) -> Iterable[str]:
if recursive:
for root, dirs, files in os.walk(dirpath):
for f in files:
if not os.path.isfile(os.path.join(root, f)):
continue
extension = os.path.splitext(f)[1][1:]
if extension.lower() in SUPPORTED_FORMATS:
yield os.path.join(root, f)
else:
with os.scandir(dirpath) as directory:
for f in directory:
if not os.path.isfile(os.path.normpath(f)):
continue
extension = os.path.splitext(f)[1][1:]
if extension.lower() in SUPPORTED_FORMATS:
yield os.path.normpath(f)
@click.command()
@click.argument('directory', type=click.Path(exists=True))
@click.option('-r', '--recursive', is_flag=True, help='Recursive')
@click.option('-s', '--speed', default='kitten', help='Speed')
def main(directory, recursive=False, speed='kitten'):
if which('cjxl') is None:
print('cjxl not found')
exit(1)
jobs = 1
try:
jobs = len(os.sched_getaffinity(0)) - 1
except Exception:
pass
try:
jobs = int(os.environ['NUMBER_OF_PROCESSORS']) - 1
except Exception:
pass
total = 0
num = 0
if recursive:
print('Processing recursively starting from', directory)
recursive = True
else:
print('Processing non-recursively starting from', directory)
recursive = False
if not os.access(directory, os.W_OK) or not os.path.exists(directory):
print('No such directory or not writable')
sys.exit(1)
for filepath in search_files(str(directory), recursive=recursive):
fp = pathlib.PurePath(filepath)
newpath = fp.parent.joinpath(fp.stem + '.' + 'jxl')
convert_cmd = ['cjxl', '--quiet', '-s', speed, f'--num_threads={jobs}', fp, newpath]
conversion_return_code = run(convert_cmd).returncode
if conversion_return_code == 0:
saved = os.path.getsize(fp) - os.path.getsize(newpath)
total += saved
num += 1
print(newpath, 'ready, saved', round(saved / 1024), 'KB')
else:
print(fp, 'error')
print('Total saved', round(total / 1024 / 1024), 'MB in', num, 'files')
if __name__ == '__main__':
main()