Skip to content

Commit abbb921

Browse files
author
Logan Endes
committed
NEF and HEIC files upload correctly
1 parent d0d5c4b commit abbb921

4 files changed

Lines changed: 38 additions & 29 deletions

File tree

gallery/file_modules/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def generate_thumbnail(self):
9898
# classism
9999
def parse_file_info(file_path: str, dir_path: str) -> Tuple[str, Optional[FileModule]]:
100100
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)
101107
mime_type = magic.from_file(file_path, mime=True)
102108
print(mime_type)
103109
print(file_path)

gallery/file_modules/heic.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
2-
from wand.image import Image
3-
from wand.color import Color
2+
from PIL import Image as PILImage
3+
import pillow_heif
44

55
from gallery.file_modules import FileModule
66
from gallery.util import hash_file
77

8+
pillow_heif.register_heif_opener()
89

910
class HEICFile(FileModule):
1011
def __init__(self, file_path, dir_path):
@@ -16,20 +17,14 @@ def __init__(self, file_path, dir_path):
1617
def generate_thumbnail(self):
1718
self.thumbnail_uuid = hash_file(self.file_path) + ".jpg"
1819

19-
with Image(filename=self.file_path) as img:
20-
with Image(width=img.width, height=img.height, background=Color("#EEEEEE")) as bg:
21-
# Fix orientation from EXIF
22-
img.auto_orient()
23-
# Force proper colorspace conversion
24-
img.transform_colorspace('srgb')
25-
# Remove alpha if present
26-
img.alpha_channel = 'remove'
27-
# Strip ICC/HDR metadata (prevents weird color shifts)
28-
img.strip()
29-
# Same process as other image types
30-
bg.composite(img, 0, 0)
31-
size = img.width if img.width < img.height else img.height
32-
bg.crop(width=size, height=size, gravity='center')
33-
bg.resize(256, 256)
34-
bg.format = 'jpeg'
35-
bg.save(filename=os.path.join(self.dir_path, self.thumbnail_uuid))
20+
thumb_path = os.path.join(self.dir_path, self.thumbnail_uuid)
21+
22+
img = PILImage.open(self.file_path).convert("RGB")
23+
24+
size = min(img.width, img.height)
25+
left = (img.width - size) // 2
26+
top = (img.height - size) // 2
27+
img = img.crop((left, top, left + size, top + size))
28+
29+
img = img.resize((256, 256))
30+
img.save(thumb_path, "JPEG")

gallery/file_modules/nef.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
from wand.image import Image
3-
from wand.color import Color
2+
import rawpy
3+
import imageio
44

55
from gallery.file_modules import FileModule
66
from gallery.util import hash_file
@@ -15,12 +15,15 @@ def __init__(self, file_path, dir_path):
1515

1616
def generate_thumbnail(self):
1717
self.thumbnail_uuid = hash_file(self.file_path) + ".jpg"
18+
thumb_path = os.path.join(self.dir_path, self.thumbnail_uuid)
1819

19-
with Image(filename=self.file_path) as img:
20-
with Image(width=img.width, height=img.height, background=Color("#EEEEEE")) as bg:
21-
bg.composite(img, 0, 0)
22-
size = img.width if img.width < img.height else img.height
23-
bg.crop(width=size, height=size, gravity='center')
24-
bg.resize(256, 256)
25-
bg.format = 'jpeg'
26-
bg.save(filename=os.path.join(self.dir_path, self.thumbnail_uuid))
20+
with rawpy.imread(self.file_path) as raw:
21+
rgb = raw.postprocess(output_bps=8)
22+
23+
h, w, _ = rgb.shape
24+
size = min(h, w)
25+
y = (h - size) // 2
26+
x = (w - size) // 2
27+
rgb = rgb[y:y+size, x:x+size]
28+
29+
imageio.imwrite(thumb_path, rgb)

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ Werkzeug==3.1.3
7070
wrapt==1.17.2
7171
xmltodict==0.14.2
7272
zipp==3.19.1
73+
# NEF and HEIC support
74+
pillow
75+
pillow-heif
76+
rawpy
77+
imageio

0 commit comments

Comments
 (0)