Skip to content

Commit 61ca3cb

Browse files
authored
feat(ui): add exr thumbnail support (#1035)
1 parent 0e7a2df commit 61ca3cb

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/tagstudio/qt/widgets/thumb_renderer.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import contextlib
77
import hashlib
88
import math
9+
import os
910
import zipfile
1011
from copy import deepcopy
1112
from io import BytesIO
@@ -72,6 +73,7 @@
7273
from tagstudio.qt.resource_manager import ResourceManager
7374

7475
ImageFile.LOAD_TRUNCATED_IMAGES = True
76+
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
7577

7678
logger = structlog.get_logger(__name__)
7779
Image.MAX_IMAGE_PIXELS = None
@@ -988,6 +990,36 @@ def _image_raw_thumb(filepath: Path) -> Image.Image:
988990
logger.error("Couldn't render thumbnail", filepath=filepath, error=type(e).__name__)
989991
return im
990992

993+
@staticmethod
994+
def _image_exr_thumb(filepath: Path) -> Image.Image | None:
995+
"""Render a thumbnail for a EXR image type.
996+
997+
Args:
998+
filepath (Path): The path of the file.
999+
"""
1000+
im: Image.Image | None = None
1001+
try:
1002+
# Load the EXR data to an array and rotate the color space from BGRA -> RGBA
1003+
raw_array = cv2.imread(str(filepath), cv2.IMREAD_UNCHANGED)
1004+
raw_array[..., :3] = raw_array[..., 2::-1]
1005+
1006+
# Correct the gamma of the raw array
1007+
gamma = 2.2
1008+
array_gamma = np.power(np.clip(raw_array, 0, 1), 1 / gamma)
1009+
array = (array_gamma * 255).astype(np.uint8)
1010+
1011+
im = Image.fromarray(array, mode="RGBA")
1012+
1013+
# Paste solid background
1014+
if im.mode == "RGBA":
1015+
new_bg = Image.new("RGB", im.size, color="#1e1e1e")
1016+
new_bg.paste(im, mask=im.getchannel(3))
1017+
im = new_bg
1018+
1019+
except Exception as e:
1020+
logger.error("Couldn't render thumbnail", filepath=filepath, error=type(e).__name__)
1021+
return im
1022+
9911023
@staticmethod
9921024
def _image_thumb(filepath: Path) -> Image.Image:
9931025
"""Render a thumbnail for a standard image type.
@@ -1544,6 +1576,9 @@ def _render(
15441576
ext, MediaCategories.IMAGE_VECTOR_TYPES, mime_fallback=True
15451577
):
15461578
image = self._image_vector_thumb(_filepath, adj_size)
1579+
# EXR Images -----------------------------------------------
1580+
if ext in [".exr"]:
1581+
image = self._image_exr_thumb(_filepath)
15471582
# Normal Images --------------------------------------------
15481583
else:
15491584
image = self._image_thumb(_filepath)

0 commit comments

Comments
 (0)