Skip to content

Commit 77cfa69

Browse files
authored
fix: use srctools instead of vtf2img for vtf files (#1014)
1 parent 89fc8a7 commit 77cfa69

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ dependencies = [
2525
"rawpy~=0.24",
2626
"Send2Trash~=1.8",
2727
"SQLAlchemy~=2.0",
28+
"srctools~=2.6",
2829
"structlog~=25.3",
2930
"toml~=0.10",
3031
"typing_extensions~=4.13",
3132
"ujson~=5.10",
32-
"vtf2img~=0.1",
3333
]
3434

3535
[project.optional-dependencies]

src/tagstudio/qt/widgets/thumb_renderer.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import contextlib
77
import hashlib
88
import math
9-
import struct
109
import zipfile
1110
from copy import deepcopy
1211
from io import BytesIO
@@ -17,6 +16,7 @@
1716
import cv2
1817
import numpy as np
1918
import rawpy
19+
import srctools
2020
import structlog
2121
from cv2.typing import MatLike
2222
from mutagen import MutagenError, flac, id3, mp4
@@ -47,7 +47,6 @@
4747
from PySide6.QtGui import QGuiApplication, QImage, QPainter, QPixmap
4848
from PySide6.QtPdf import QPdfDocument, QPdfDocumentRenderOptions
4949
from PySide6.QtSvg import QSvgRenderer
50-
from vtf2img import Parser
5150

5251
from tagstudio.core.constants import (
5352
FONT_SAMPLE_SIZES,
@@ -631,27 +630,22 @@ def _blender(filepath: Path) -> Image.Image:
631630
return im
632631

633632
@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.
636635
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.
638640
"""
639-
parser = Parser(filepath)
640-
im: Image.Image = None
641+
im: Image.Image | None = None
641642
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()
652646

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__)
655649
return im
656650

657651
@staticmethod
@@ -1446,7 +1440,7 @@ def _render(
14461440
elif MediaCategories.is_ext_in_category(
14471441
ext, MediaCategories.SOURCE_ENGINE_TYPES, mime_fallback=True
14481442
):
1449-
image = self._source_engine(_filepath)
1443+
image = self._vtf_thumb(_filepath)
14501444
# No Rendered Thumbnail ========================================
14511445
if not image:
14521446
raise NoRendererError

0 commit comments

Comments
 (0)