Skip to content

Commit 140d4cd

Browse files
author
Logan Endes
committed
Differentiate NEF from TIFF by checking if the file is parsable by rawpy instead of by extensions
1 parent abbb921 commit 140d4cd

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

gallery/file_modules/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any, Dict, List, Optional, Tuple
44
from wand.image import Image
55
from wand.color import Color
6+
import rawpy
67

78
from gallery.util import DEFAULT_THUMBNAIL_NAME
89
from gallery.util import hash_file
@@ -98,13 +99,18 @@ def generate_thumbnail(self):
9899
# classism
99100
def parse_file_info(file_path: str, dir_path: str) -> Tuple[str, Optional[FileModule]]:
100101
print("entering parse_file_info")
101-
ext = os.path.splitext(file_path)[-1].lower()
102-
# .nef is a RAW file
103-
if ext == ".nef": # .nef is a special case, magic reads it as .tiff, but it is not processed correctly by the tiff module
104-
print("image/x-nikon-nef")
105-
print(file_path)
106-
return "image/x-nikon-nef", NEFFile(file_path, dir_path)
102+
107103
mime_type = magic.from_file(file_path, mime=True)
104+
105+
if "tif" in mime_type: # .nef is a special case, magic reads it as .tiff, but it is not processed correctly by the tiff module
106+
# check if it is a raw file
107+
try:
108+
with rawpy.imread(file_path): # this may cause issues for other raw files
109+
mime_type = "image/x-nikon-nef"
110+
except rawpy.LibRawFileUnsupportedError:
111+
pass
112+
except rawpy.LibRawIOError:
113+
pass
108114
print(mime_type)
109115
print(file_path)
110116

requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ wrapt==1.17.2
7171
xmltodict==0.14.2
7272
zipp==3.19.1
7373
# NEF and HEIC support
74-
pillow
75-
pillow-heif
76-
rawpy
77-
imageio
74+
pillow==11.1.0
75+
pillow_heif==1.2.0
76+
rawpy==0.26.1
77+
imageio==2.4.0

0 commit comments

Comments
 (0)