forked from defold/defold.github.io
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_webp.py
42 lines (35 loc) · 1.32 KB
/
convert_to_webp.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
from pathlib import Path
from PIL import Image
import os.path
from termcolor import colored
total_saved_size = 0
def sizeof_fmt(num, suffix="B"):
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
def convert_to_webp_if_need(source):
global total_saved_size
destination = source.with_suffix(".webp")
if not os.path.isfile(destination):
image = Image.open(source)
size_before = os.path.getsize(source)
image.save(destination, format="webp", method=6, lossless=True)
size_after = os.path.getsize(destination)
diff = size_before - size_after
color = "green" if diff > 0 else "red"
print("Convert to WEBP: " + str(destination) + " " + colored(sizeof_fmt(diff), color))
total_saved_size = total_saved_size + diff
return destination
def main():
global total_saved_size
paths = Path(".").glob("**/*.png")
for path in paths:
convert_to_webp_if_need(path)
# paths = Path(".").glob("**/*.jpg")
# for path in paths:
# convert_to_webp_if_need(path)
color = "green" if total_saved_size > 0 else "red"
print(colored("Total saved size is "+sizeof_fmt(total_saved_size), color))
main()