Add ability to auto-detect double pages

Co-authored-by: Sven Hesse <drmccoy@drmccoy.de>
This commit is contained in:
Timmy Welch 2024-07-27 15:57:11 -07:00
parent f10ceb3216
commit 63832606b1
2 changed files with 20 additions and 3 deletions

View File

@ -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))

View File

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