-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavif.py
146 lines (118 loc) · 7.03 KB
/
avif.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
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
import config
import helpers_web as wh
import helpers_web as hw
import os
from PIL import Image, ImageOps
if __name__ == "__main__":
def test_compression():
out_exts = [".webp", ".avif", ".png"]
image_paths = [
config.project_folder + "wp-content/themes/karlsruhe-digital/images/beitragsseite_hero_slider.jpg",
# config.project_folder + "wp-content/themes/karlsruhe-digital/images/programm_hero.jpg",
#config.project_folder + "wp-content/themes/karlsruhe-digital/images/bloguebersichtseite_hero_slider.jpg",
# config.project_folder + "wp-content/themes/karlsruhe-digital/images/suchseite_hero_slider.jpg",
# config.project_folder + "wp-content/themes/karlsruhe-digital/images/suchergebnisse_hero_slider.jpg",
config.project_folder + "wp-content/uploads/2019/08/1-IT-Cluster-in-Europa.png",
#"__tmp__images_compression/__girl.png",
]
for path in image_paths:
for quality in range(0, 101, 10):
print("\n"*4 + "#"*88)
image = Image.open(path)
is_transp = wh.image_has_transparency(image)
old_size = image.size
image.thumbnail((1600, 1600), resample=Image.Resampling.LANCZOS)
#__image_smaller_png_path = os.path.abspath("__smaller.png")
# if os.path.isfile(__image_smaller_png_path):
# os.remove(__image_smaller_png_path)
import tempfile
__image_smaller_png_path = tempfile.NamedTemporaryFile(suffix='.png').name
image.save(__image_smaller_png_path, format="png")
for new_ext in out_exts:
print("-"*66)
name, ext = os.path.splitext(path)
base = os.path.basename(name)
q = f"_q{quality:03d}"
out_path = "__tmp__images_compression/_PIL/" + base + q + "_PIL" + new_ext
out_path_IM = "__tmp__images_compression/_IMA/" + base + q + "_IMA" + new_ext
out_path_AV = "__tmp__images_compression/_AVE/" + base + q + "_AVE" + new_ext
out_path_LL = "__tmp__images_compression/_LLS/" + base + q + "_LLS" + new_ext
wh.make_dirs(out_path)
wh.make_dirs(out_path_IM)
wh.make_dirs(out_path_AV)
wh.make_dirs(out_path_LL)
print("", "quality:", wh.YELLOW, quality, wh.MAGENTA, out_path, wh.RESET)
# print("\t", "is_transp:", is_transp)
# print("\t", "size :", old_size, "-->", image.size)
format = new_ext.lstrip('.')
# if is_transp:
# image.save(out_path, format=format, optimize=True, lossless=True) # !!! need great alpha
# else:
image.save(out_path, format=format, optimize=True, quality=quality)
# lossless
image.save(out_path_LL, format=format, optimize=True, lossless=True)
print("-"*66)
# magick -quality 40 -define heic:speed=0 test.jpg test.avif
# https://askubuntu.com/questions/1411869/creating-avif-or-heic-images-with-transparency-does-not-work-with-imagemagick-in
import subprocess
print("magick...")
subprocess.call([
"magick", "convert",
"-quality", str(quality), # https://imagemagick.org/script/command-line-options.php#quality
os.path.abspath(__image_smaller_png_path),
os.path.abspath(out_path_IM)
])
print("magick: done")
"""
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpeg" alt="Description of the image">
</picture>
"""
# #./avifenc [options] input.file output.avif
# # https://web.dev/compress-images-avif/
# assert os.path.isfile(os.path.abspath("avif/avifenc.exe"))
# assert os.path.isfile(os.path.abspath(path))
# color QP [0 (Lossless) <-> 63 (Worst)],
# alpha QP [0 (Lossless) <-> 63 (Worst)]
# https://medium.com/yavar/avif-the-nextgen-image-format-91162caf32d2
if format == "avif":
print("-"*66)
print(wh.YELLOW, "avifenc...", format, wh.RESET)
min_val = 0
max_val = 63
q_av = int(wh.map(quality, 0, 100, max_val, min_val))
print("q_av", q_av)
"""
--advanced color:cq-level={q_av} # does NOT work
--advanced alpha:cq-level=10 # does NOT work
--advanced cq-level=10
--advanced cq-level={q_av}
"""
# avifenc [options] input.[jpg|jpeg|png|y4m] output.avif
cmd = f"""
{os.path.abspath("avif/avifenc.exe")}
--speed {5}
--jobs {8}
--min {min_val} --max {max_val}
--minalpha {min_val} --maxalpha {max_val}
--advanced end-usage=q
--advanced cq-level={q_av}
--advanced tune=ssim
--advanced sharpness=0
{os.path.abspath(__image_smaller_png_path)}
{os.path.abspath(out_path_AV)}
"""
##md = f""" {os.path.abspath("avif/avifenc.exe")} --help """
cmd = wh.string_remove_whitespace(cmd)
print(wh.CYAN, end='')
print(cmd)
ret = os.system(wh.dq(cmd))
print(wh.RESET, end='')
print("-"*66)
wh.image_show_file(out_path, secs=0)
os.remove(__image_smaller_png_path)
test_compression()
exit(0)