Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save "nclx_profile" in a HEIC image? #118

Closed
wiggin15 opened this issue Jul 20, 2023 · 7 comments
Closed

How to save "nclx_profile" in a HEIC image? #118

wiggin15 opened this issue Jul 20, 2023 · 7 comments
Labels
bug Something isn't working enhancement New feature or request fixed Fixed in last version

Comments

@wiggin15
Copy link

I'm trying to add the "nclx_profile" metadata to a new HEIC file.
I'm setting this key as a dict in pil_image.info, but when I save and load the photo the data isn't there.

Full example code to reproduce:

import numpy as np
import pillow_heif
from PIL import Image
from io import BytesIO

from pillow_heif import register_heif_opener
register_heif_opener()

nclx_profile = {
 'color_primaries': 1,
 'transfer_characteristics': 2,
 'matrix_coefficients': 1,
 'full_range_flag': 1,
 'color_primary_red_x': 0.0,
 'color_primary_red_y': 0.0,
 'color_primary_green_x': 0.0,
 'color_primary_green_y': 0.0,
 'color_primary_blue_x': 0.0,
 'color_primary_blue_y': 0.0,
 'color_primary_white_x': 0.0,
 'color_primary_white_y': 0.0
}

# create a new, blank image
pil_image = Image.fromarray(np.ones((16, 16, 3)).astype(np.uint8))
# sset nclx_profile
pil_image.info['nclx_profile'] = nclx_profile
# save to an in-memory file (works if we save to disk too)
tmp = BytesIO()
pil_image.save(tmp, format="heif")
# re-open the image and see if nclx_profile is there
Image.open(tmp).info['nclx_profile']    # KeyError!
@bigcat88
Copy link
Owner

I am not sure that it is not set, need to check, earlier versions of libheif was able to set it, but was not able to retrieve it after set, as libheif does not support reading of nclx embedded in stream and not in image.
And during encoding libheif embeds it in stream.
I will take a look today at this.

@bigcat88
Copy link
Owner

https://github.com/strukturag/libheif/blob/794a791b5048ff2bcdb6507c4413719652f568cf/libheif/heif.h#L997-L1002

When libheif will add such ability, I immediately will add tests for this in pillow_heif, as I myself waiting for it.

@wiggin15
Copy link
Author

Are you saying that the NCLX data is indeed stored, but not read?

I have a HEIC file (that I can't share, unfortunately) where there are multiple images and each of them does have the information read correctly by pillow-heif

from pillow_heif import open_heif
i = open_heif("myfile.heic")
i[0].info['nclx_profile']  # working
i[1].info['nclx_profile']  # working

I'm trying to construct a similar file using:

primary_pil.save(..., append_images=[secondary_pil])

and putting the nclx_profile on both PILs' info.
I'm not entirely sure how my HEIC was generated but I'd be surprised if they didn't use libheif themselves...

@bigcat88
Copy link
Owner

This is real, a perfect issue.

Thank you for pointing on this.
Need to set macOS_compatibility_workaround_no_nclx_profile flag to false, to make it works.
But in current version it is not possible.

Will try to do a fix for a new one that is coming.

@bigcat88 bigcat88 added bug Something isn't working enhancement New feature or request labels Jul 20, 2023
@bigcat88 bigcat88 added the fixed in upcoming release fix will arrive with next release label Jul 20, 2023
@bigcat88
Copy link
Owner

Done. New version will be published, as soon as Python3.12 RC1 will come and cibuildwheel will get update for it(early August).

@bigcat88 bigcat88 added fixed Fixed in last version and removed fixed in upcoming release fix will arrive with next release labels Aug 9, 2023
@bigcat88
Copy link
Owner

bigcat88 commented Aug 9, 2023

done, new version is out

@bigcat88
Copy link
Owner

P.S.: In new upcoming version #171 NCLX is saved by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request fixed Fixed in last version
Projects
None yet
Development

No branches or pull requests

2 participants