|
8 | 8 | from copy import deepcopy |
9 | 9 | from io import BytesIO |
10 | 10 | from pathlib import Path |
| 11 | +import struct |
11 | 12 |
|
12 | 13 | import cv2 |
13 | 14 | import numpy as np |
|
39 | 40 | from src.qt.helpers.gradient import four_corner_gradient |
40 | 41 | from src.qt.helpers.text_wrapper import wrap_full_text |
41 | 42 | from src.qt.resource_manager import ResourceManager |
| 43 | +from vtf2img import Parser |
42 | 44 |
|
43 | 45 | ImageFile.LOAD_TRUNCATED_IMAGES = True |
44 | 46 |
|
@@ -600,6 +602,33 @@ def _blender(self, filepath: Path) -> Image.Image: |
600 | 602 | ) |
601 | 603 | return im |
602 | 604 |
|
| 605 | + def _source_engine(self, filepath: Path) -> Image.Image: |
| 606 | + """ |
| 607 | + This is a function to convert the VTF (Valve Texture Format) files to thumbnails, it works using the VTF2IMG library for PILLOW. |
| 608 | + """ |
| 609 | + parser = Parser(filepath) |
| 610 | + im: Image.Image = None |
| 611 | + try: |
| 612 | + im = parser.get_image() |
| 613 | + |
| 614 | + except ( |
| 615 | + AttributeError, |
| 616 | + UnidentifiedImageError, |
| 617 | + FileNotFoundError, |
| 618 | + TypeError, |
| 619 | + struct.error, |
| 620 | + ) as e: |
| 621 | + if str(e) == "expected string or buffer": |
| 622 | + logging.info( |
| 623 | + f"[ThumbRenderer][VTF][INFO] {filepath.name} Doesn't have an embedded thumbnail. ({type(e).__name__})" |
| 624 | + ) |
| 625 | + |
| 626 | + else: |
| 627 | + logging.error( |
| 628 | + f"[ThumbRenderer][VTF][ERROR]: Couldn't render thumbnail for {filepath.name} ({type(e).__name__})" |
| 629 | + ) |
| 630 | + return im |
| 631 | + |
603 | 632 | def _font_short_thumb(self, filepath: Path, size: int) -> Image.Image: |
604 | 633 | """Render a small font preview ("Aa") thumbnail from a font file. |
605 | 634 |
|
@@ -961,6 +990,10 @@ def render( |
961 | 990 | elif MediaType.BLENDER in MediaCategories.get_types(ext): |
962 | 991 | image = self._blender(_filepath) |
963 | 992 |
|
| 993 | + # VTF ========================================================== |
| 994 | + elif MediaType.SOURCE_ENGINE in MediaCategories.get_types(ext): |
| 995 | + image = self._source_engine(_filepath) |
| 996 | + |
964 | 997 | # No Rendered Thumbnail ======================================== |
965 | 998 | if not image: |
966 | 999 | raise UnidentifiedImageError |
|
0 commit comments