|
6 | 6 | import contextlib |
7 | 7 | import hashlib |
8 | 8 | import math |
9 | | -import struct |
10 | 9 | import zipfile |
11 | 10 | from copy import deepcopy |
12 | 11 | from io import BytesIO |
|
17 | 16 | import cv2 |
18 | 17 | import numpy as np |
19 | 18 | import rawpy |
| 19 | +import srctools |
20 | 20 | import structlog |
21 | 21 | from cv2.typing import MatLike |
22 | 22 | from mutagen import MutagenError, flac, id3, mp4 |
|
47 | 47 | from PySide6.QtGui import QGuiApplication, QImage, QPainter, QPixmap |
48 | 48 | from PySide6.QtPdf import QPdfDocument, QPdfDocumentRenderOptions |
49 | 49 | from PySide6.QtSvg import QSvgRenderer |
50 | | -from vtf2img import Parser |
51 | 50 |
|
52 | 51 | from tagstudio.core.constants import ( |
53 | 52 | FONT_SAMPLE_SIZES, |
@@ -631,27 +630,22 @@ def _blender(filepath: Path) -> Image.Image: |
631 | 630 | return im |
632 | 631 |
|
633 | 632 | @staticmethod |
634 | | - def _source_engine(filepath: Path) -> Image.Image: |
635 | | - """This is a function to convert the VTF (Valve Texture Format) files to thumbnails. |
| 633 | + def _vtf_thumb(filepath: Path) -> Image.Image | None: |
| 634 | + """Extract and render a thumbnail for VTF (Valve Texture Format) images. |
636 | 635 |
|
637 | | - It works using the VTF2IMG library for PILLOW. |
| 636 | + Uses the srctools library for reading VTF files. |
| 637 | +
|
| 638 | + Args: |
| 639 | + filepath (Path): The path of the file. |
638 | 640 | """ |
639 | | - parser = Parser(filepath) |
640 | | - im: Image.Image = None |
| 641 | + im: Image.Image | None = None |
641 | 642 | try: |
642 | | - im = parser.get_image() |
643 | | - |
644 | | - except ( |
645 | | - AttributeError, |
646 | | - UnidentifiedImageError, |
647 | | - TypeError, |
648 | | - struct.error, |
649 | | - ) as e: |
650 | | - if str(e) == "expected string or buffer": |
651 | | - logger.error("Couldn't render thumbnail", filepath=filepath, error=type(e).__name__) |
| 643 | + with open(filepath, "rb") as f: |
| 644 | + vtf = srctools.VTF.read(f) |
| 645 | + im = vtf.get(frame=0).to_PIL() |
652 | 646 |
|
653 | | - else: |
654 | | - logger.error("Couldn't render thumbnail", filepath=filepath, error=type(e).__name__) |
| 647 | + except ValueError as e: |
| 648 | + logger.error("Couldn't render thumbnail", filepath=filepath, error=type(e).__name__) |
655 | 649 | return im |
656 | 650 |
|
657 | 651 | @staticmethod |
@@ -1446,7 +1440,7 @@ def _render( |
1446 | 1440 | elif MediaCategories.is_ext_in_category( |
1447 | 1441 | ext, MediaCategories.SOURCE_ENGINE_TYPES, mime_fallback=True |
1448 | 1442 | ): |
1449 | | - image = self._source_engine(_filepath) |
| 1443 | + image = self._vtf_thumb(_filepath) |
1450 | 1444 | # No Rendered Thumbnail ======================================== |
1451 | 1445 | if not image: |
1452 | 1446 | raise NoRendererError |
|
0 commit comments