-
Notifications
You must be signed in to change notification settings - Fork 10
/
__init__.py
88 lines (70 loc) · 2.33 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
import pyheif
from PIL import Image, ImageFile
from pyheif.error import HeifError
class HeifImageFile(ImageFile.ImageFile):
format = 'HEIF'
format_description = "HEIF/HEIC image"
def _open(self):
data = self.fp.read(16)
if not check_heif_magic(data):
raise SyntaxError('not a HEIF file')
self.fp.seek(0)
try:
heif_file = pyheif.read(self.fp)
except HeifError as e:
raise SyntaxError(str(e))
# size in pixels (width, height)
self._size = heif_file.size
# mode setting
self.mode = heif_file.mode
# Add Exif
if heif_file.metadata:
for data in heif_file.metadata:
if data['type'] == 'Exif':
self.info['exif'] = data['data']
break
offset = self.fp.tell()
self.tile = [
('heif', (0, 0) + self.size, offset, (heif_file,))
]
class HeifDecoder(ImageFile.PyDecoder):
_pulls_fd = True
def decode(self, buffer):
heif_file = self.args[0]
mode = heif_file.mode
raw_decoder = Image._getdecoder(mode, 'raw', (mode, heif_file.stride))
raw_decoder.setimage(self.im)
return raw_decoder.decode(heif_file.data)
def check_heif_magic(data):
magic1 = data[4:8]
magic2 = data[8:12]
# https://github.com/strukturag/libheif/issues/83
# https://github.com/GNOME/gimp/commit/e4bff4c8016f18195f9a6229f59cbf41740ddb8d
# 'heic': the usual HEIF images
# 'heix': 10bit images, or anything that uses h265 with range extension
# 'hevc', 'hevx': brands for image sequences
# 'heim': multiview
# 'heis': scalable
# 'hevm': multiview sequence
# 'hevs': scalable sequence
# 'hevs': scalable sequence
code_list = [
# Other
b'heic',
b'heix',
b'hevc',
b'hevx',
b'heim',
b'heis',
b'hevm',
b'hevs',
# iPhone
b'mif1',
]
return magic1 == b'ftyp' or magic2 in code_list
def register_heif_opener():
Image.register_open(HeifImageFile.format, HeifImageFile, check_heif_magic)
Image.register_decoder('heif', HeifDecoder)
Image.register_extensions(HeifImageFile.format, ['.heic', '.heif'])
Image.register_mime(HeifImageFile.format, 'image/heif')