Update db_operator.py

Refactoring image code
This commit is contained in:
José Manuel Barroso Galindo
2023-04-11 23:30:29 +02:00
committed by GitHub
parent 3a7f82a70d
commit 0be700b295

View File

@@ -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]: