|
| 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. |
0 commit comments