Skip to content

Commit 5268af7

Browse files
committed
optimization: do not copy image data twice for appended images
1 parent 3aacb3d commit 5268af7

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

pillow_heif/heif.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,7 @@ def save(self, fp, **kwargs) -> None:
657657

658658
if not options().hevc_enc:
659659
raise HeifError(code=HeifErrorCode.ENCODING_ERROR, subcode=5000, message="No encoder found.")
660-
save_all = kwargs.get("save_all", True)
661-
images_to_append = kwargs.get("append_images", [])
662-
append_one_image = not self.images and not save_all
663-
images_to_save = self.images + self.__heif_images_from(images_to_append, append_one_image)
664-
if not save_all:
665-
images_to_save = images_to_save[:1]
660+
images_to_save = self.__get_images_for_save(self.images, **kwargs)
666661
if not images_to_save:
667662
raise ValueError("Cannot write file with no images as HEIF.")
668663
primary_index = kwargs.get("primary_index", None)
@@ -715,12 +710,11 @@ def _save(ctx: LibHeifCtxWrite, img_list: List[HeifImage], primary_index: int, *
715710
enc_options = lib.heif_encoding_options_alloc()
716711
enc_options = ffi.gc(enc_options, lib.heif_encoding_options_free)
717712
for i, img in enumerate(img_list):
718-
new_img = create_image(img.size, img.chroma, img.bit_depth, img.data, stride=img.stride)
719-
set_color_profile(new_img, img.info)
720-
p_new_img_handle = ffi.new("struct heif_image_handle **")
721-
error = lib.heif_context_encode_image(ctx.ctx, new_img, ctx.encoder, enc_options, p_new_img_handle)
713+
set_color_profile(img.heif_img, img.info)
714+
p_img_handle = ffi.new("struct heif_image_handle **")
715+
error = lib.heif_context_encode_image(ctx.ctx, img.heif_img, ctx.encoder, enc_options, p_img_handle)
722716
check_libheif_error(error)
723-
new_img_handle = ffi.gc(p_new_img_handle[0], lib.heif_image_handle_release)
717+
new_img_handle = ffi.gc(p_img_handle[0], lib.heif_image_handle_release)
724718
exif = img.info["exif"]
725719
xmp = img.info["xmp"]
726720
if i == primary_index:
@@ -739,7 +733,7 @@ def _save(ctx: LibHeifCtxWrite, img_list: List[HeifImage], primary_index: int, *
739733
p_new_thumb_handle = ffi.new("struct heif_image_handle **")
740734
error = lib.heif_context_encode_thumbnail(
741735
ctx.ctx,
742-
new_img,
736+
img.heif_img,
743737
new_img_handle,
744738
ctx.encoder,
745739
enc_options,
@@ -751,18 +745,21 @@ def _save(ctx: LibHeifCtxWrite, img_list: List[HeifImage], primary_index: int, *
751745
lib.heif_image_handle_release(p_new_thumb_handle[0])
752746

753747
@staticmethod
754-
def __heif_images_from(images: list, load_one: bool) -> List[HeifImage]:
748+
def __get_images_for_save(images: List[HeifImage], **kwargs) -> List[HeifImage]:
755749
"""Accepts list of Union[HeifFile, HeifImage, Image.Image] and returns List[HeifImage]"""
756750

751+
images_to_save = images + list(kwargs.get("append_images", []))
752+
save_one = not kwargs.get("save_all", True)
753+
if save_one:
754+
images_to_save = images_to_save[:1]
757755
result = []
758-
for img in images:
756+
for img in images_to_save:
759757
if isinstance(img, Image.Image):
760-
heif_file = HeifFile().add_from_pillow(img, load_one, thumbs_no_data=True)
758+
heif_file = HeifFile().add_from_pillow(img, save_one, thumbs_no_data=True)
761759
else:
762-
heif_file = HeifFile().add_from_heif(img, load_one, thumbs_no_data=True)
760+
no_primary = not bool(img in images)
761+
heif_file = HeifFile().add_from_heif(img, save_one, no_primary, thumbs_no_data=True)
763762
result += list(heif_file)
764-
if load_one:
765-
break
766763
return result
767764

768765

0 commit comments

Comments
 (0)