Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bigcat88 committed Jun 16, 2022
1 parent df1bb3f commit 5f9cb99
Show file tree
Hide file tree
Showing 8 changed files with 949 additions and 332 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

All notable changes to this project will be documented in this file.

## [0.3.1 - 2022-06-17]

### Added

- (Heif) - `HeifFile` gets `images: List[HeifImage]` and alternative method of changing order of images by editing it.
- (HeifImagePlugin) - `info` image dictionary can be now edited in place and it will be saved for image sequences.

### Changed

- Updated docs.

### Fixed

- (HeifImagePlugin) Again fixing image order, for Pillow plugin it was not fixed fully in 0.3.0.
- Optimizing code.

## [0.3.0 - 2022-06-11]

### Added
Expand Down
3 changes: 2 additions & 1 deletion docs/cooking-heif-file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Starting from version `0.3.1` all images are in public list, and you can swap th
Saving images
-------------

Refer to :py:meth:`~pillow_heif.HeifFile.save` to see what additional parameters is supported.
Refer to :py:meth:`~pillow_heif.HeifFile.save` to see what additional parameters is supported
and to :ref:`encoding` for some explanations.

.. code-block:: python
Expand Down
31 changes: 25 additions & 6 deletions docs/encoding.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _encoding:

Encoding of images
==================

Expand All @@ -10,31 +12,34 @@ Current limitations of encoder
Metadata encoding
"""""""""""""""""

All known metadata information in ``image.info`` dictionary are saved to output image.
All known metadata information in ``info`` dictionary are saved to output image(for both `Pillow` plugin and `HeifFile`).
Those are:
``exif``, ``xmp`` and ``metadata``

So this is valid code for removing EXIF information:
So this is valid code for removing EXIF and XMP information:

.. code-block:: python
image = Image.open(Path("test.heic"))
image.info["exif"] = None
image.info["xmp"] = None
image.save("output.heic")
And this code is valid too:

.. code-block:: python
image = Image.open(Path("test.heic"))
image.save("output.heic", exif=None)
image.save("output.heic", exif=None, xmp=None)
Limitations of second code variant is that when file has multiply images inside,
setting ``exif`` during save affects only Primary(Main) image and not all images.
setting ``exif`` or ``xmp`` during ``save`` affects only Primary(Main) image and not all images.

To edit metadata of all images in a file just iterate throw all images and change metadata in place.

To edit metadata of all images in a file just iterate throw all images and change metadata you want(do not work for Pillow plugin).
Here are two ways for `Pillow`:

When you want edit metadata when using as Pillow plugin for all images you can do something like this(editing ``info["exif"]`` field of each image):
For example edit ``info["exif"]`` field of each image copy:

.. code-block:: python
Expand All @@ -47,11 +52,21 @@ When you want edit metadata when using as Pillow plugin for all images you can d
empty_pillow = Image.new("P", (0, 0))
empty_pillow.save("no_exif.heic", save_all=True, append_images=output_wo_exif)
Or editing ``info["exif"]`` in place(from version `0.3.1`):

.. code-block:: python
heic_pillow = Image.open(Path("test.heic"))
for frame in ImageSequence.Iterator(heic_pillow):
frame.info["exif"] = None
heic_pillow.save("no_exif.heic", save_all=True)
.. _changing-order-of-images:

Changing order of images
""""""""""""""""""""""""

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

.. code-block:: python
Expand All @@ -68,6 +83,10 @@ Now as example lets change primary image in a HEIC file:
img1 = Image.open(Path("1_2P_3.heic"))
img1.save("1_2_3P.heic", save_all=True, primary_index=-1, quality=-1)
.. note::

As a ``primary`` field are in `info` dictionary, you can change it in a place like with metadata before.

And here is an example how we can change order of images in container:

.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion pillow_heif/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" Version of pillow_heif """

__version__ = "0.3.0"
__version__ = "0.3.1"
35 changes: 12 additions & 23 deletions pillow_heif/as_opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ._options import options
from .constants import HeifErrorCode
from .error import HeifError
from .heif import HeifFile, HeifImage, is_supported, open_heif
from .heif import HeifFile, is_supported, open_heif
from .misc import getxmp, set_orientation


Expand All @@ -23,6 +23,7 @@ class HeifImageFile(ImageFile.ImageFile):
_close_exclusive_fp_after_loading = False

def __init__(self, *args, **kwargs):
self.__frame = 0
self.heif_file = None
super().__init__(*args, **kwargs)

Expand All @@ -33,12 +34,12 @@ def _open(self):
raise SyntaxError(str(exception)) from None
self.custom_mimetype = heif_file.mimetype
self.heif_file = heif_file
self._init_from_heif_file(heif_file)
self._init_from_heif_file(self.__frame)
self.tile = []

def load(self):
if self.heif_file:
frame_heif = self._heif_image_by_index(self.tell())
frame_heif = self.heif_file[self.tell()]
self.load_prepare()
truncated = False
try:
Expand Down Expand Up @@ -69,16 +70,11 @@ def getxmp(self) -> dict:
def seek(self, frame):
if not self._seek_check(frame):
return
self._init_from_heif_file(self._heif_image_by_index(frame))
self.__frame = frame
self._init_from_heif_file(frame)

def tell(self):
i = 0
if self.heif_file:
for heif in self.heif_file:
if self.info["img_id"] == heif.info["img_id"]:
break
i += 1
return i
return self.__frame

def verify(self) -> None:
for _ in self.info["thumbnails"]:
Expand All @@ -103,18 +99,11 @@ def _seek_check(self, frame):
raise EOFError("attempt to seek outside sequence")
return self.tell() != frame

def _heif_image_by_index(self, index) -> HeifImage:
return self.heif_file[index]

def _init_from_heif_file(self, heif_image) -> None:
self._size = heif_image.size
self.mode = heif_image.mode
for k in ("exif", "xmp", "metadata", "primary", "img_id"):
self.info[k] = heif_image.info[k]
for k in ("icc_profile", "icc_profile_type", "nclx_profile"):
if k in heif_image.info:
self.info[k] = heif_image.info[k]
self.info["thumbnails"] = heif_image.thumbnails
def _init_from_heif_file(self, img_index: int) -> None:
self._size = self.heif_file[img_index].size
self.mode = self.heif_file[img_index].mode
self.info = self.heif_file[img_index].info
self.info["thumbnails"] = self.heif_file[img_index].thumbnails
self.info["original_orientation"] = set_orientation(self.info)


Expand Down
Loading

0 comments on commit 5f9cb99

Please sign in to comment.