1+ import os
2+ from wand .image import Image
3+ from wand .color import Color
4+
5+ from gallery .file_modules import FileModule
6+ from gallery .util import hash_file
7+
8+
9+ class HEICFile (FileModule ):
10+ def __init__ (self , file_path , dir_path ):
11+ FileModule .__init__ (self , file_path , dir_path )
12+ self .mime_type = "image/heic"
13+
14+ self .generate_thumbnail ()
15+
16+ def generate_thumbnail (self ):
17+ self .thumbnail_uuid = hash_file (self .file_path ) + ".jpg"
18+
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 ))
0 commit comments