Skip to content

Commit 61f7258

Browse files
committed
v0.5.0[docs]
1 parent 1d05ec5 commit 61f7258

File tree

11 files changed

+172
-131
lines changed

11 files changed

+172
-131
lines changed

docs/encoding.rst

Lines changed: 0 additions & 40 deletions
This file was deleted.

docs/heif-file.rst

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ Opening
99
if pillow_heif.is_supported("image.heif"):
1010
heif_file = pillow_heif.open_heif("image.heif")
1111
12+
``open_heif`` is preferred over ``read_heif``, it does not decode images immediatly.
13+
All image data supports `lazy loading` and will be automatically decoded when you request it,
14+
e.g. when access to ``data`` or ``stride`` properties occurs.
15+
1216
Creating from Pillow
1317
--------------------
1418

1519
.. code-block:: python
1620
1721
heif_file = pillow_heif.from_pillow(Image.open("image.gif")):
1822
23+
You can specify ``load_one=True`` if you need to add only one frame from a multi-frame image.
24+
1925
Creating from bytes
2026
-------------------
2127

@@ -39,6 +45,8 @@ Enumerating images
3945
for img in heif_file:
4046
print(img)
4147
48+
.. note:: ``HeifFile`` itself points to the primary image in the container.
49+
4250
Adding images
4351
-------------
4452

@@ -90,10 +98,10 @@ Starting from version `0.3.1` all images are in public list, and you can swap th
9098
9199
heif_file.images[0], heif_file.images[1] = heif_file.images[1], heif_file.images[0]
92100
93-
Saving images
94-
-------------
101+
Saving
102+
------
95103

96-
Refer to :py:meth:`~pillow_heif.HeifFile.save` to see what additional parameters is supported and to :ref:`encoding`.
104+
Refer to :py:meth:`~pillow_heif.HeifFile.save` to see what additional parameters is supported and to :ref:`saving-images`.
97105

98106
.. code-block:: python
99107
@@ -107,6 +115,8 @@ Accessing image data
107115
Decoded image data from ``libheif`` available throw :py:attr:`~pillow_heif.HeifImage.data` property
108116
with the help of :py:attr:`~pillow_heif.HeifImage.stride` property.
109117

118+
Accessing `Primary` image in a file:
119+
110120
.. code-block:: python
111121
112122
print(len(heif_file.data), heif_file.stride)
@@ -123,12 +133,19 @@ Or you can access each image by index:
123133
Numpy array interface
124134
---------------------
125135

126-
Next code gets decoded image data as a numpy array(in the same format as ``Pillow`` does):
136+
Next code gets decoded primary image data as a numpy array(in the same format as ``Pillow`` does):
137+
138+
.. code-block:: python
139+
140+
heif_file = pillow_heif.open_heif("file.heif")
141+
np_array = np.asarray(heif_file)
142+
143+
Accessing image by index(for multi-frame images):
127144

128145
.. code-block:: python
129146
130147
heif_file = pillow_heif.open_heif("file.heif")
131-
np_array = np.asarray(heif_file[0])
148+
np_array = np.asarray(heif_file[0]) # accessing image by index.
132149
133150
After that you can load it at any library that supports numpy arrays.
134151

docs/index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ Pillow-Heif documentation
1818
heif-file.rst
1919
image-modes.rst
2020
options.rst
21-
encoding.rst
21+
saving-images.rst
2222
thumbnails.rst
2323
reference/index.rst
24-
v0.3-order-of-images.rst
2524
workaround-orientation.rst
2625
benchmarks.rst
2726

docs/pillow-plugin.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Image Modes
4646
Currently all images are opened in ``RGB`` or ``RGBA`` 8 bit modes.
4747
There is a restriction in ``libheif`` that we cant check before decoding if an image is ``monochrome`` or not.
4848

49-
See here :ref:`image-modes` for a list of supported modes for saving.
49+
See :ref:`image-modes` for a list of supported modes for saving.
5050

5151
Metadata
5252
********
@@ -120,7 +120,7 @@ Also images can be saved to memory, using ``format`` parameter:
120120
See here :ref:`save-parameters` for additional information.
121121

122122
Changing order of images
123-
""""""""""""""""""""""""
123+
************************
124124

125125
There is no such easy way to change order as for `HeifFile` usage, but the standard Pillow way to do so looks fine.
126126
Let's create image where second image will be primary:

docs/reference/API.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ Opening HEIF file
1111
.. autofunction:: read_heif
1212
.. autofunction:: from_pillow
1313
.. autofunction:: from_bytes
14+
15+
Thumbnails
16+
----------
17+
1418
.. autofunction:: thumbnail
19+
.. autofunction:: add_thumbnails
1520

1621
Low Level API
1722
-------------

docs/reference/HeifImage.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ HeifImage object
5050

5151
A boolean value that specifies whether the image is the main image when the file
5252
contains more than one image.
53+
54+
.. py:attribute:: thumbnails
55+
:type: list
56+
57+
List of thumbnails(:class:`HeifThumbnail`) present for this image. Can be empty.

docs/saving-images.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. _saving-images:
2+
3+
Saving images
4+
=============
5+
6+
Current limitations of encoder
7+
""""""""""""""""""""""""""""""
8+
9+
* ``Libheif`` does not support editing files in place(it is not possible just to change metadata)
10+
* ``HEIF`` format does not provide information in what quality image was encoded
11+
12+
13+
.. _save-parameters:
14+
15+
Save parameters
16+
"""""""""""""""
17+
18+
Method ``save`` in both ``HeifFile`` and ``Pillow`` supports the **same** parameters.
19+
There is only two differences between them:
20+
21+
* Pillow's ``save`` by default has ``save_all=False`` and ``HeifFile`` has ``save_all=True``
22+
* When saving to memory for ``HeifFile`` you do not need to specify ``format`` parameter.
23+
24+
Here is description of it: :py:meth:`~pillow_heif.HeifFile.save`
25+
26+
.. _saving-16bit:
27+
28+
Saving 16 bit images
29+
""""""""""""""""""""
30+
31+
All 16 bit images that was created with:
32+
33+
* :py:meth:`~pillow_heif.HeifImage.convert_to`
34+
* :py:meth:`~pillow_heif.HeifFile.add_from_pillow`
35+
* :py:meth:`~pillow_heif.HeifFile.add_frombytes`
36+
* or images opened in ``I`` Pillow modes when using as a Pillow plugin
37+
38+
Will be saved by default in 10 bit mode.
39+
40+
To ``save`` 16 bit image in 12 bit for you can convert it to 12 bit before saving or set ``options().save_to_12bit`` to ``True``.
41+
42+
.. _order-of-images:
43+
44+
Order Of Images
45+
"""""""""""""""
46+
47+
All information here is only for files that has multiply images and when first image in file is not a `PrimaryImage`
48+
49+
There was a slightly different behaviour in 0.2.x versions and 0.3.0 - 0.4.0 versions.
50+
Starting from version `0.5.0` ``pillow-heif`` in both ``Pillow`` and ``stand alone`` mode works the same way.
51+
52+
Lets imagine that we have file with 3 images and second image in file is a primary image.
53+
54+
.. code-block:: python
55+
56+
im = open_heif("image.heif")
57+
im_pil = Image.open("image.heif")
58+
59+
Both ``im`` and ``im_pil`` points to the second image in file, that is a primary image.
60+
61+
This asserts will pass:
62+
63+
.. code-block:: python
64+
65+
assert im.info["primary"] == True
66+
assert im[0].info["primary"] == False
67+
assert im[1].info["primary"] == True
68+
assert im[2].info["primary"] == False
69+
70+
assert im_pil.info["primary"] == True
71+
im_pil.seek(0)
72+
assert im_pil.info["primary"] == False
73+
im_pil.seek(1)
74+
assert im_pil.info["primary"] == True
75+
im_pil.seek(2)
76+
assert im_pil.info["primary"] == False
77+
78+
And next code will produce the same behavior results(during enumerating all frames):
79+
80+
.. code-block:: python
81+
82+
for frame in im:
83+
print(frame.mode, frame.size)
84+
85+
for frame in ImageSequence.Iterator(im_pil):
86+
print(frame.mode, frame.size)
87+
88+
Primary image when saving image sequences
89+
"""""""""""""""""""""""""""""""""""""""""
90+
91+
First image will be `primary`, unless after first image there is no images with `info["primary"]` set to ``True``.
92+
So, last image with `info["primary"]` == ``True`` will be the primary image.
93+
94+
In most cases you do not need to bother yourself with this, as other image formats are much simpler and has no such abilities.
95+
96+
When you need to change `primary` image for some reason, just set `info["primary"]` of image to ``True``
97+
and all images after that should not have `info["primary"]` == ``True``.
98+
99+
Method ``save`` supports ``primary_index`` parameter, that accepts ``index of image`` or ``-1`` to set last image as `PrimaryImage`.
100+
101+
Specifying ``primary_index`` during ``save`` has highest priority.
102+
103+
That's all.

docs/thumbnails.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,34 @@ Or as a Pillow plugin:
3131
Function :py:func:`~pillow_heif.thumbnail` has an optional parameter ``min_box``,
3232
so you can specify minimal size of thumbnail you interested in.
3333
Images that come from iPhone usual has thumbnails with 512 box size.
34+
35+
Adding thumbnails
36+
"""""""""""""""""
37+
38+
For adding thumbnails use :py:func:`~pillow_heif.add_thumbnails`.
39+
40+
It accepts both ``HeifImage`` or ``PIL.Image.Image`` and also can accept ``HeifFile``
41+
42+
When input is a ``HeifFile`` it will add thumbnails to all images in that file.
43+
44+
When you adding thumbnails, if image has already such thumbnails sizes they wil be skipped.
45+
46+
There is two examples for adding thumbnails in `examples` folder.
47+
48+
Removing thumbnails
49+
"""""""""""""""""""
50+
51+
For Pillow you can clear list with thumbnails:
52+
53+
.. code-block:: python
54+
55+
im.info["thumbnails"].clear()
56+
57+
For ``HeifFile`` or ``HeifImage``:
58+
59+
.. code-block:: python
60+
61+
im.thumbnails.clear()
62+
63+
Or use ``del im.info["thumbnails"][index]`` for ``Pillow``
64+
and ``del im.thumbnails[index]`` for ``HeifImage`` to remove only one thumbnail.

docs/v0.3-order-of-images.rst

Lines changed: 0 additions & 79 deletions
This file was deleted.

pillow_heif/heif.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ def open_heif(fp, convert_hdr_to_8bit=True) -> HeifFile:
808808
def read_heif(fp, convert_hdr_to_8bit=True) -> HeifFile:
809809
"""Opens the given HEIF image file and decodes all images.
810810
811-
.. note:: In most cases it better to call :py:meth:`~pillow_heif.open_heif`, and
811+
.. note:: In most cases it is better to call :py:meth:`~pillow_heif.open_heif`, and
812812
let images decoded automatically only when needed.
813813
814814
:param fp: See parameter ``fp`` in :func:`is_supported`

0 commit comments

Comments
 (0)