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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
Metadata-Version: 2.1
Name: pillow_heif
Version: 0.21.0
Summary: Python interface for libheif library
Home-page: https://github.com/bigcat88/pillow_heif
Author: Alexander Piskun
Author-email: bigcat88@users.noreply.github.com
License: BSD-3-Clause
Project-URL: Documentation, https://pillow-heif.readthedocs.io
Project-URL: Source, https://github.com/bigcat88/pillow_heif
Project-URL: Changelog, https://github.com/bigcat88/pillow_heif/blob/master/CHANGELOG.md
Keywords: heif,heic,avif,pillow
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: License :: OSI Approved :: GNU General Public License v2 (GPLv2)
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: Microsoft :: Windows
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE.txt
License-File: LICENSES_bundled.txt
Requires-Dist: pillow>=10.1.0
Provides-Extra: docs
Requires-Dist: sphinx>=4.4; extra == "docs"
Requires-Dist: sphinx-issues>=3.0.1; extra == "docs"
Requires-Dist: sphinx-rtd-theme>=1.0; extra == "docs"
Provides-Extra: tests-min
Requires-Dist: pytest; extra == "tests-min"
Requires-Dist: defusedxml; extra == "tests-min"
Requires-Dist: packaging; extra == "tests-min"
Provides-Extra: tests
Requires-Dist: pytest; extra == "tests"
Requires-Dist: defusedxml; extra == "tests"
Requires-Dist: packaging; extra == "tests"
Requires-Dist: numpy; extra == "tests"
Requires-Dist: pympler; extra == "tests"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: defusedxml; extra == "dev"
Requires-Dist: packaging; extra == "dev"
Requires-Dist: numpy; extra == "dev"
Requires-Dist: pympler; extra == "dev"
Requires-Dist: opencv-python==4.10.0.84; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Requires-Dist: pylint; extra == "dev"
Requires-Dist: coverage; extra == "dev"
# pillow-heif
[](https://github.com/bigcat88/pillow_heif/actions/workflows/analysis-coverage.yml)
[](https://github.com/bigcat88/pillow_heif/actions/workflows/nightly-src-build.yml)
[](https://github.com/bigcat88/pillow_heif/actions/workflows/test-wheels.yml)
[](https://pillow-heif.readthedocs.io/en/latest/?badge=latest)
[](https://codecov.io/gh/bigcat88/pillow_heif)



[](https://pepy.tech/project/pillow-heif)
[](https://pepy.tech/project/pillow-heif)





Python bindings to [libheif](https://github.com/strukturag/libheif) for working with HEIF images and plugin for Pillow.
Features:
* Decoding of `8`, `10`, `12` bit HEIC and AVIF files.
* Encoding of `8`, `10`, `12` bit HEIC and AVIF files.
* `EXIF`, `XMP`, `IPTC` read & write support.
* Support of multiple images in one file and a `PrimaryImage` attribute.
* Adding & removing `thumbnails`.
* Reading of `Depth Images`.
* (beta) Reading of `Auxiliary Images` by [johncf](https://github.com/johncf)
* Adding HEIF support to Pillow in one line of code as a plugin.
Note: Here is a light version [pi-heif](https://pypi.org/project/pi-heif/) of this project without encoding capabilities.
### Install
```console
python3 -m pip install -U pip
python3 -m pip install pillow-heif
```
### Example of use as a Pillow plugin
```python3
from PIL import Image
from pillow_heif import register_heif_opener
register_heif_opener()
im = Image.open("image.heic") # do whatever need with a Pillow image
im = im.rotate(13)
im.save(f"rotated_image.heic", quality=90)
```
### 16 bit PNG to 10 bit HEIF using OpenCV
```python3
import cv2
import pillow_heif
cv_img = cv2.imread("16bit_with_alpha.png", cv2.IMREAD_UNCHANGED)
heif_file = pillow_heif.from_bytes(
mode="BGRA;16",
size=(cv_img.shape[1], cv_img.shape[0]),
data=bytes(cv_img)
)
heif_file.save("RGBA_10bit.heic", quality=-1)
```
### 8/10/12 bit HEIF to 8/16 bit PNG using OpenCV
```python3
import numpy as np
import cv2
import pillow_heif
heif_file = pillow_heif.open_heif("image.heic", convert_hdr_to_8bit=False, bgr_mode=True)
np_array = np.asarray(heif_file)
cv2.imwrite("image.png", np_array)
```
### Accessing decoded image data
```python3
import pillow_heif
if pillow_heif.is_supported("image.heic"):
heif_file = pillow_heif.open_heif("image.heic", convert_hdr_to_8bit=False)
print("image size:", heif_file.size)
print("image mode:", heif_file.mode)
print("image data length:", len(heif_file.data))
print("image data stride:", heif_file.stride)
```
### Get decoded image data as a Numpy array
```python3
import numpy as np
import pillow_heif
if pillow_heif.is_supported("input.heic"):
heif_file = pillow_heif.open_heif("input.heic")
np_array = np.asarray(heif_file)
```
### AVIF support
Working with the `AVIF` files as the same as with the `HEIC` files. Just use a separate function to register plugin:
```python3
import pillow_heif
pillow_heif.register_avif_opener()
```
### Accessing Depth Images
```python3
from PIL import Image
from pillow_heif import register_heif_opener
import numpy as np
register_heif_opener()
im = Image.open("../tests/images/heif_other/pug.heic")
if im.info["depth_images"]:
depth_im = im.info["depth_images"][0] # Access the first depth image (usually there will be only one).
# Depth images are instances of `class HeifDepthImage(BaseImage)`,
# so work with them as you would with any usual image in pillow_heif.
# Depending on what you need the depth image for, you can convert it to a NumPy array or convert it to a Pillow image.
pil_im = depth_im.to_pillow()
np_im = np.asarray(depth_im)
print(pil_im)
print(pil_im.info["metadata"])
```
### More Information
- [Documentation](https://pillow-heif.readthedocs.io/)
- [Installation](https://pillow-heif.readthedocs.io/en/latest/installation.html)
- [Pillow plugin](https://pillow-heif.readthedocs.io/en/latest/pillow-plugin.html)
- [Using HeifFile](https://pillow-heif.readthedocs.io/en/latest/heif-file.html)
- [Image modes](https://pillow-heif.readthedocs.io/en/latest/image-modes.html)
- [Options](https://pillow-heif.readthedocs.io/en/latest/options.html)
- [Examples](https://github.com/bigcat88/pillow_heif/tree/master/examples)
- [Contribute](https://github.com/bigcat88/pillow_heif/blob/master/.github/CONTRIBUTING.md)
- [Discussions](https://github.com/bigcat88/pillow_heif/discussions)
- [Issues](https://github.com/bigcat88/pillow_heif/issues)
- [Changelog](https://github.com/bigcat88/pillow_heif/blob/master/CHANGELOG.md)
### Wheels
| **_Wheels table_** | macOS<br/>Intel | macOS<br/>Silicon | Windows<br/> | musllinux* | manylinux* |
|--------------------|:---------------:|:-----------------:|:------------:|:----------:|:----------:|
| CPython 3.9 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.10 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.11 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.12 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.13 | ✅ | ✅ | ✅ | ✅ | ✅ |
| PyPy 3.9 v7.3 | ✅ | ✅ | ✅ | N/A | ✅ |
| PyPy 3.10 v7.3 | ✅ | ✅ | ✅ | N/A | ✅ |
* **x86_64**, **aarch64** wheels.
|