|
52 | 52 | get_size, |
53 | 53 | process_image, |
54 | 54 | ) |
55 | | -from .settings import Status, get_thumb |
| 55 | +from .settings import IMG_EXTENSIONS, Status, get_thumb |
56 | 56 | from .utils import ( |
57 | 57 | Devnull, |
58 | 58 | check_or_create_dir, |
@@ -246,15 +246,10 @@ def __init__(self, filename, path, settings): |
246 | 246 | super().__init__(filename, path, settings) |
247 | 247 | imgformat = settings.get("img_format") |
248 | 248 |
|
249 | | - # Register all formats |
250 | | - PILImage.init() |
251 | | - |
252 | | - if imgformat and PILImage.EXTENSION[self.src_ext] != imgformat.upper(): |
| 249 | + if imgformat and IMG_EXTENSIONS.ext2format[self.src_ext] != imgformat.upper(): |
253 | 250 | # Find the extension that should match img_format |
254 | | - extensions = {v: k for k, v in PILImage.EXTENSION.items()} |
255 | | - ext = extensions[imgformat.upper()] |
| 251 | + ext = IMG_EXTENSIONS.format2ext[imgformat.upper()] |
256 | 252 | self.dst_filename = self.basename + ext |
257 | | - self.thumb_name = get_thumb(self.settings, self.dst_filename) |
258 | 253 |
|
259 | 254 | @cached_property |
260 | 255 | def date(self): |
@@ -593,76 +588,76 @@ def thumbnail(self): |
593 | 588 | # Test the thumbnail from the Markdown file. |
594 | 589 | thumbnail = self.meta.get("thumbnail", [""])[0] |
595 | 590 |
|
596 | | - if thumbnail and isfile(join(self.src_path, thumbnail)): |
597 | | - self._thumbnail = url_from_path( |
598 | | - join(self.name, get_thumb(self.settings, thumbnail)) |
599 | | - ) |
| 591 | + if thumbnail: |
| 592 | + # if thumbnail is set in the markdown, it can be either the |
| 593 | + # original filename or the generated name after format conversion |
| 594 | + if isfile(join(self.src_path, thumbnail)): |
| 595 | + thumbnail = get_thumb(self.settings, thumbnail) |
| 596 | + self._thumbnail = url_from_path(join(self.name, thumbnail)) |
600 | 597 | self.logger.debug("Thumbnail for %r : %s", self, self._thumbnail) |
601 | 598 | return self._thumbnail |
602 | | - else: |
603 | | - # find and return the first landscape image |
604 | | - for f in self.medias: |
605 | | - ext = splitext(f.dst_filename)[1] |
606 | | - if ext.lower() not in self.settings["img_extensions"]: |
607 | | - continue |
608 | | - |
609 | | - # Use f.size if available as it is quicker (in cache), but |
610 | | - # fallback to the size of src_path if dst_path is missing |
611 | | - size = f.input_size |
612 | | - if size is None: |
613 | | - size = f.file_metadata["size"] |
614 | | - |
615 | | - if size["width"] > size["height"]: |
| 599 | + |
| 600 | + # find and return the first landscape image |
| 601 | + for f in self.medias: |
| 602 | + ext = splitext(f.dst_filename)[1] |
| 603 | + if ext.lower() not in self.settings["img_extensions"]: |
| 604 | + continue |
| 605 | + |
| 606 | + # Use f.size if available as it is quicker (in cache), but |
| 607 | + # fallback to the size of src_path if dst_path is missing |
| 608 | + size = f.input_size |
| 609 | + if size is None: |
| 610 | + size = f.file_metadata["size"] |
| 611 | + |
| 612 | + if size["width"] > size["height"]: |
| 613 | + try: |
| 614 | + self._thumbnail = url_quote(self.name) + "/" + f.thumbnail |
| 615 | + except Exception as e: |
| 616 | + self.logger.info( |
| 617 | + "Failed to get thumbnail for %s: %s", f.dst_filename, e |
| 618 | + ) |
| 619 | + else: |
| 620 | + self.logger.debug( |
| 621 | + "Use 1st landscape image as thumbnail for %r : %s", |
| 622 | + self, |
| 623 | + self._thumbnail, |
| 624 | + ) |
| 625 | + return self._thumbnail |
| 626 | + |
| 627 | + # else simply return the 1st media file |
| 628 | + if not self._thumbnail and self.medias: |
| 629 | + for media in self.medias: |
| 630 | + if media.thumbnail is not None: |
616 | 631 | try: |
617 | | - self._thumbnail = url_quote(self.name) + "/" + f.thumbnail |
| 632 | + self._thumbnail = url_quote(self.name) + "/" + media.thumbnail |
618 | 633 | except Exception as e: |
619 | 634 | self.logger.info( |
620 | | - "Failed to get thumbnail for %s: %s", f.dst_filename, e |
| 635 | + "Failed to get thumbnail for %s: %s", |
| 636 | + media.dst_filename, |
| 637 | + e, |
621 | 638 | ) |
622 | 639 | else: |
623 | | - self.logger.debug( |
624 | | - "Use 1st landscape image as thumbnail for %r : %s", |
625 | | - self, |
626 | | - self._thumbnail, |
627 | | - ) |
628 | | - return self._thumbnail |
629 | | - |
630 | | - # else simply return the 1st media file |
631 | | - if not self._thumbnail and self.medias: |
632 | | - for media in self.medias: |
633 | | - if media.thumbnail is not None: |
634 | | - try: |
635 | | - self._thumbnail = ( |
636 | | - url_quote(self.name) + "/" + media.thumbnail |
637 | | - ) |
638 | | - except Exception as e: |
639 | | - self.logger.info( |
640 | | - "Failed to get thumbnail for %s: %s", |
641 | | - media.dst_filename, |
642 | | - e, |
643 | | - ) |
644 | | - else: |
645 | | - break |
646 | | - else: |
647 | | - self.logger.warning("No thumbnail found for %r", self) |
648 | | - return |
| 640 | + break |
| 641 | + else: |
| 642 | + self.logger.warning("No thumbnail found for %r", self) |
| 643 | + return |
649 | 644 |
|
650 | | - self.logger.debug( |
651 | | - "Use the 1st image as thumbnail for %r : %s", self, self._thumbnail |
652 | | - ) |
653 | | - return self._thumbnail |
654 | | - |
655 | | - # use the thumbnail of their sub-directories |
656 | | - if not self._thumbnail: |
657 | | - for path, album in self.gallery.get_albums(self.path): |
658 | | - if album.thumbnail: |
659 | | - self._thumbnail = url_quote(self.name) + "/" + album.thumbnail |
660 | | - self.logger.debug( |
661 | | - "Using thumbnail from sub-directory for %r : %s", |
662 | | - self, |
663 | | - self._thumbnail, |
664 | | - ) |
665 | | - return self._thumbnail |
| 645 | + self.logger.debug( |
| 646 | + "Use the 1st image as thumbnail for %r : %s", self, self._thumbnail |
| 647 | + ) |
| 648 | + return self._thumbnail |
| 649 | + |
| 650 | + # use the thumbnail of their sub-directories |
| 651 | + if not self._thumbnail: |
| 652 | + for path, album in self.gallery.get_albums(self.path): |
| 653 | + if album.thumbnail: |
| 654 | + self._thumbnail = url_quote(self.name) + "/" + album.thumbnail |
| 655 | + self.logger.debug( |
| 656 | + "Using thumbnail from sub-directory for %r : %s", |
| 657 | + self, |
| 658 | + self._thumbnail, |
| 659 | + ) |
| 660 | + return self._thumbnail |
666 | 661 |
|
667 | 662 | self.logger.error("Thumbnail not found for %r", self) |
668 | 663 |
|
|
0 commit comments