Skip to content

Commit d7921c2

Browse files
committed
Add logos_check.py to validate logos filesize/resolution
1 parent 0042917 commit d7921c2

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tools/logos_check.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import argparse
5+
from pathlib import Path
6+
7+
from PIL import Image
8+
9+
10+
class ImageCheck():
11+
def __init__(self, imgpath: Path) -> None:
12+
self.imgpath = imgpath
13+
self.image = Image.open(imgpath)
14+
15+
self.fails: list[str] = []
16+
17+
def check(self) -> bool:
18+
self.fails.clear()
19+
for fn_name in dir(self):
20+
if fn_name.startswith("check_"):
21+
getattr(self, fn_name)()
22+
23+
if not self.fails:
24+
return True
25+
26+
print(f"Image '{self.imgpath}' failed tests:")
27+
for fail in self.fails:
28+
print(f" - {fail}")
29+
print()
30+
return False
31+
32+
def check_type(self) -> None:
33+
format = self.image.format.lower()
34+
accepted_formats = ["png", "jpeg", "webp"]
35+
if format not in accepted_formats:
36+
self.fails.append(f"Image should be one of {', '.join(accepted_formats)} but is {format}")
37+
38+
def check_dimensions(self) -> None:
39+
dim_min, dim_max = 96, 300
40+
dimensions_range = range(dim_min, dim_max+1)
41+
w, h = self.image.width, self.image.height
42+
if w not in dimensions_range or h not in dimensions_range:
43+
self.fails.append(f"Dimensions should be in [{dim_min}, {dim_max}] but are {w}×{h}")
44+
45+
def check_ratio(self) -> None:
46+
w, h = self.image.width, self.image.height
47+
if w != h:
48+
self.fails.append(f"Image is not square but {w}×{h}")
49+
50+
def check_filesize(self) -> None:
51+
filesize = self.imgpath.stat().st_size
52+
max_size = 80_000
53+
if filesize > max_size:
54+
self.fails.append(f"Filesize should be <={max_size/1000}kB but is {filesize/1000}kB")
55+
56+
def check_compressed(self) -> None:
57+
pass
58+
59+
60+
def main() -> None:
61+
parser = argparse.ArgumentParser()
62+
parser.add_argument("logos", nargs="+", type=Path, help="Logos to check")
63+
args = parser.parse_args()
64+
65+
total_result = True
66+
for logo in args.logos:
67+
checker = ImageCheck(logo)
68+
if checker.check() is not True:
69+
total_result = False
70+
71+
if not total_result:
72+
sys.exit(1)
73+
74+
if __name__ == "__main__":
75+
main()

0 commit comments

Comments
 (0)