From 52da7038290eb74156bb3cd46ffd75036ed5f298 Mon Sep 17 00:00:00 2001 From: Max Meinhold Date: Thu, 20 May 2021 17:19:00 -0400 Subject: [PATCH] Enable HEIC files --- gallery/file_modules/__init__.py | 2 ++ gallery/file_modules/heic.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 gallery/file_modules/heic.py diff --git a/gallery/file_modules/__init__.py b/gallery/file_modules/__init__.py index 39917ab..b7bdc84 100644 --- a/gallery/file_modules/__init__.py +++ b/gallery/file_modules/__init__.py @@ -55,6 +55,7 @@ def generate_thumbnail(self): from gallery.file_modules.cr2 import CR2File from gallery.file_modules.png import PNGFile from gallery.file_modules.gif import GIFFile +from gallery.file_modules.heic import HEICFile from gallery.file_modules.bmp import BMPFile from gallery.file_modules.tiff import TIFFFile from gallery.file_modules.mp4 import MP4File @@ -70,6 +71,7 @@ def generate_thumbnail(self): "image/x-canon-cr2": CR2File, "image/png": PNGFile, "image/gif": GIFFile, + "image/heic": HEICFILE, "image/bmp": BMPFile, "image/x-ms-bmp": BMPFile, "image/x-windows-bmp": BMPFile, diff --git a/gallery/file_modules/heic.py b/gallery/file_modules/heic.py new file mode 100644 index 0000000..0029eb8 --- /dev/null +++ b/gallery/file_modules/heic.py @@ -0,0 +1,27 @@ +import os +from wand.image import Image +from wand.color import Color + +from gallery.file_modules import FileModule +from gallery.util import hash_file + + +class HEICFile(FileModule): + + def __init__(self, file_path, dir_path): + FileModule.__init__(self, file_path, dir_path) + self.mime_type = "image/heic" + + self.generate_thumbnail() + + def generate_thumbnail(self): + self.thumbnail_uuid = hash_file(self.file_path) + ".jpg" + + with Image(filename=self.file_path) as img: + with Image(width=img.width, height=img.height, background=Color("#EEEEEE")) as bg: + bg.composite(img, 0, 0) + size = img.width if img.width < img.height else img.height + bg.crop(width=size, height=size, gravity='center') + bg.resize(256, 256) + bg.format = 'jpeg' + bg.save(filename=os.path.join(self.dir_path, self.thumbnail_uuid))