From 0be700b29568a38af709a387dda7ca9e73e88a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Manuel=20Barroso=20Galindo?= Date: Tue, 11 Apr 2023 23:30:29 +0200 Subject: [PATCH] Update db_operator.py Refactoring image code --- .github/db_operator.py | 44 +++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/.github/db_operator.py b/.github/db_operator.py index a7fb03742..ff0bf1a0a 100755 --- a/.github/db_operator.py +++ b/.github/db_operator.py @@ -312,16 +312,14 @@ class Tags: self._append(result, self._use_term('filters_video')) elif parent in ['wallpapers']: - try: - ar = image_aspect_ratio(path) - if abs(ar - 1.77) < 0.1: - self._append(result, self._use_term('ar16:9')) - elif abs(ar - 1.33) < 0.1: - self._append(result, self._use_term('ar4:3')) + ar = read_image_aspect_ratio(path) + if ar is None: + pass + elif abs(ar - 1.77) < 0.1: + self._append(result, self._use_term('ar16:9')) + elif abs(ar - 1.33) < 0.1: + self._append(result, self._use_term('ar4:3')) - except Exception as e: - print('wallpaper image not opened: ' + str(path)) - print(e) self._append(result, self._use_term(stem)) return result @@ -450,15 +448,6 @@ class Tags: result.append(entry) return sorted(result) -def image_aspect_ratio(path: Path) -> float: - try: - from PIL import Image - except ImportError as _e: - subprocess.run([sys.executable, '-m', 'pip', 'install', 'Pillow'], stderr=subprocess.STDOUT, check=True) - from PIL import Image - img = Image.open(str(path)) - return float(img.width) / float(img.height) - class DatabaseBuilder: main_binaries = ['MiSTer', 'menu.rbf'] @@ -870,6 +859,25 @@ def _read_mgl_fields_impl(mgl_path: Path) -> Tuple[Optional[str], Optional[str]] return rbf, setname +# Read other files + +def read_image_aspect_ratio(path: Path) -> Optional[float]: + try: + _ensure_image_library() + from PIL import Image + img = Image.open(str(path)) + return float(img.width) / float(img.height) + except Exception as e: + print('wallpaper image not opened: ' + str(path)) + print(e) + return None + +def _ensure_image_library() -> None: + try: + from PIL import Image + except ImportError as _e: + subprocess.run([sys.executable, '-m', 'pip', 'install', 'Pillow'], stderr=subprocess.STDOUT, check=True) + # MiSTer network utilities def download_db(url: str) -> Dict[str, Any]: