Skip to content

Commit 4dc60d4

Browse files
authored
pre-commit: Check for images exceeding 4096×4096 (#2207)
As documented in https://github.com/endlessm/threadbare/wiki/Visual-Style-Guide#image-file-format this is a limitation of mobile GPUs. Check all PNGs, JPEGs and WebPs against this limit using a small Python program using Pillow. Update the GitHub Actions workflow to check out objects from LFS.
1 parent 4cc95c9 commit 4dc60d4

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

.github/workflows/checks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
runs-on: ubuntu-latest
2020
steps:
2121
- uses: actions/checkout@v6
22+
- uses: ./.github/actions/lfs-cached-checkout
2223
- uses: tox-dev/action-pre-commit-uv@v1
2324

2425
reuse:

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ repos:
3939
scenes/quests/story_quests/(?!stella/)
4040
)
4141
42+
- repo: local
43+
hooks:
44+
- id: check-image-size
45+
name: check image dimensions
46+
entry: python tools/check-image-size.py
47+
language: python
48+
additional_dependencies: [Pillow]
49+
types_or: [png, jpeg, webp]
50+
4251
- repo: https://github.com/Scony/godot-gdscript-toolkit
4352
rev: 4.5.0
4453
hooks:

tools/check-image-size.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
# /// script
3+
# dependencies = [
4+
# "pillow>=12.2.0",
5+
# ]
6+
# ///
7+
8+
# SPDX-FileCopyrightText: The Threadbare Authors
9+
# SPDX-License-Identifier: MPL-2.0
10+
"""
11+
Fail if any given image file exceeds 4096px wide or 4096px high.
12+
"""
13+
14+
import argparse
15+
import sys
16+
17+
from PIL import Image, UnidentifiedImageError
18+
19+
# https://docs.godotengine.org/en/stable/tutorials/3d/3d_rendering_limitations.html
20+
# “if you want your texture to display correctly on all platforms, you should
21+
# avoid using textures larger than 4096×4096”
22+
MAX_SIZE = 4096
23+
24+
25+
def check(path) -> bool:
26+
try:
27+
with Image.open(path) as im:
28+
width, height = im.size
29+
except (UnidentifiedImageError, OSError) as e:
30+
print(f"{path}: could not read image ({e})", file=sys.stderr)
31+
return False
32+
33+
if width > MAX_SIZE or height > MAX_SIZE:
34+
print(
35+
f"{path}: {width}x{height} exceeds {MAX_SIZE}x{MAX_SIZE}",
36+
file=sys.stderr,
37+
)
38+
return False
39+
40+
return True
41+
42+
43+
def main() -> int:
44+
parser = argparse.ArgumentParser(description=__doc__)
45+
parser.add_argument("files", nargs="*")
46+
args = parser.parse_args()
47+
48+
return not all([check(path) for path in args.files])
49+
50+
51+
if __name__ == "__main__":
52+
sys.exit(main())

0 commit comments

Comments
 (0)