From 63832606b1291ae1bf1e09fbfbf7601d67392afe Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sat, 27 Jul 2024 15:57:11 -0700 Subject: [PATCH] Add ability to auto-detect double pages Co-authored-by: Sven Hesse --- comicapi/comicarchive.py | 13 +++++++++++-- comicapi/utils.py | 10 +++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index 5ff9681..32f264d 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -331,7 +331,9 @@ class ComicArchive: self.page_count = len(self.get_page_name_list()) return self.page_count - def apply_archive_info_to_metadata(self, md: GenericMetadata, calc_page_sizes: bool = False) -> None: + def apply_archive_info_to_metadata( + self, md: GenericMetadata, calc_page_sizes: bool = False, detect_double_page: bool = False + ) -> None: md.page_count = self.get_number_of_pages() if calc_page_sizes: @@ -345,7 +347,12 @@ class ComicArchive: self.pil_available = True except ImportError: self.pil_available = False - if "size" not in p or "height" not in p or "width" not in p: + if ( + "size" not in p + or "height" not in p + or "width" not in p + or ("double_page" not in p and detect_double_page) + ): data = self.get_page(idx) if data: try: @@ -358,6 +365,8 @@ class ComicArchive: p["size"] = str(len(data)) p["height"] = str(h) p["width"] = str(w) + if detect_double_page: + p["double_page"] = utils.is_double_page(p) except Exception as e: logger.warning("Error decoding image [%s] %s :: image %s", e, self.path, index) p["size"] = str(len(data)) diff --git a/comicapi/utils.py b/comicapi/utils.py index b522f20..dac74bc 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -26,7 +26,7 @@ from collections import defaultdict from collections.abc import Iterable, Mapping from enum import Enum, auto from shutil import which # noqa: F401 -from typing import Any, TypeVar, cast +from typing import TYPE_CHECKING, Any, TypeVar, cast from comicfn2dict import comicfn2dict @@ -36,6 +36,8 @@ from comicapi._url import LocationParseError as LocationParseError # noqa: F401 from comicapi._url import Url as Url from comicapi._url import parse_url as parse_url +if TYPE_CHECKING: + from comicapi.genericmetadata import ImageMetadata try: import icu @@ -109,6 +111,12 @@ def _custom_key(tup: Any) -> Any: T = TypeVar("T") +def is_double_page(page: ImageMetadata) -> bool: + w = int(page.get("width", 0)) + h = int(page.get("height", 0)) + return page.get("double_page") or (w >= h and w > 0 and h > 0) + + def os_sorted(lst: Iterable[T]) -> Iterable[T]: import natsort