-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Support for HEIF #2806
Comments
I would welcome support, but I'm not aware of any appropriately licensed HEIF or HEVC libraries to do encoding/decoding. |
The reference HEVC encoder/decoder is licensed as BSD 3-clause (but obviously extremely slow and tuned for PSNR), the openHEVC decoder is licensed as LGPL2.1. Patent licenses are the bigger issue; the current patent licensing situation with 3 competing pools means that it'd be practically impossible for any FOSS project to produce binaries including a HEVC decoder which don't violate some patents. So even if someone were to do the work to implement it, you wouldn't find it in most Pillow builds, because nobody wants to get harassed by the patent trolls. |
The HEIF container decoder is also not a free license: https://github.com/nokiatech/heif/blob/master/LICENSE.TXT . |
There's an initial HEIF support patch for FFmpeg floating around on their mailing list, but as the post itself says, it's early work and the whole format is batshit insane. The container is based off of MP4 for a start. I don't think it'll be possible to support HEIF in Pillow in a sane manner based on the container and codec complexity alone, and even if someone were to make a demuxer+decoder in one library with a license Pillow could use, you wouldn't be able to build a product on it for around 30 more years, assuming the patents remain unlicenseable for anyone without big money lawyers. |
I'm definitely -1 on batshit insane. I think that the only way it could work is if we shipped support that was dynamically linked and based off of OS libraries, e.g. CoreImage on OSX. But it's not clear that would work, and it would only be the one platform at this point. I'm going to close this, if the world comes around to the point that it's reasonable, we can look at it. |
What about libheif? It takes care of the packaging while using libde265 for reading and x265 for writing. All three libraries are LGPL. My understanding is that there would be no patent issues whatsoever for Pillow. The HEIF container itself has a blanket patent waiver, and the HEVC licensing authority doesn't require a license for software that isn't bundled with devices.
As far as I can tell, there's nothing blocking this other than the current instability of libheif's API. Edit: Found a better link for the royalty-free license announcement and added a summary. |
libheif encoder API is stable since version 1.1.0. It will appear in Debian testing in a few days: I have not much experience with python, but if you need any assistance, feel free to contact me (I'm the libheif author). |
@rspeed HEVC Advance is only one of three competing HEVC licensing authorities. |
@CounterPillow So we'd need similar guarantees from the others? |
Yes, or just hope none of them feel like trying to enforce their patents against you. Though I don't think patent issues should prevent HEIF support from being added to Pillow. It's up to the companies or individuals building commercials products using Pillow to ensure their product doesn't infringe on any patents; I was simply pointing out that Linux distros like RedHat or OpenSuSE likely will want to ship Pillow without HEIF support. |
It seems Nokia released an open source HEIF Javascript reader, and also C libraries to read and write HEIF: https://github.com/nokiatech/heif/wiki . Wouldn't it be possible to use this to add support to the library? |
@ncalligaro Unfortunately no, see above: #2806 (comment). |
Sorry, why are you not using libheif? |
May be I wrong, but this license only applies to Nokia's software, not the HEIF container format.
libheif is not based on Nokia`s code and don't uses Nokia's library. This is an independent implementation of decoder/encoder of HEIF images under GPLv3 license. This library are included in the official repositories of Debian, Ubuntu and other Linux distros. |
@Cykooz I feel like this has already been mentioned - #2806 (comment) |
@radarhere I'm not sure what you're referring to. Nothing I said in that comment disagrees with the point @Cykooz was making. |
I think it's time to implement libheif if not as a decoder only. I'm noticing a lot of products out there support the conversion of heic files to jpg but not the other way around. I think this is how they are getting away with not worrying about patent issues, etc... But as @rspeed noted in #2806 , there are no issues if you are using the format in the application layer as long as the device already has a license in it's initial software:
ImageMagick is using it so why not Pillow. |
This issue, I think, very definitely needs revisiting in 2020. A large percentage of all image/video files in the world are now generated, managed and stored natively in heic/heif format. The python community needs a tool for coping with this reality. |
If anyone needs a workaround for this, I managed to open a HEIC file using the pyheif library and convert it to a Pillow Image like this: from PIL import Image
import pyheif
heif_file = pyheif.read_heif("IMG_7424.HEIC")
image = Image.frombytes(mode=heif_file.mode, size=heif_file.size, data=heif_file.data)
image.save("IMG_7424.jpg", "JPEG") |
is this something that PIL might support in the future? |
This issue, I think, needs revisiting in 2021 ;) |
Finally I probably get this working. If you use this I can confirm it works. |
I just tried using pyheif_pillow_opener, but can't get it to work. >>> from PIL import Image
>>> from pyheif_pillow_opener import register_heif_opener
>>> register_heif_opener()
>>> img = Image.open(".../heif.heic")
...
PIL.UnidentifiedImageError: cannot identify image file I'm using a Ubuntu 20.04 derivative, pyheif 0.5.1, libheif1 1.11.0, and libde265-0 1.0.8. |
@mara004 I would suggest asking that over at https://github.com/ciotto/pyheif-pillow-opener |
Yeah, good point. I'll do that. |
You can also try my package https://pypi.org/project/cykooz.heif from PIL import Image
from cykooz.heif.pil import register_heif_opener
register_heif_opener()
img = Image.open('data/test.heif')
assert isinstance(img, Image.Image)
assert img.size == (3024, 4032)
assert img.mode == 'RGB'
assert img.getpixel((100, 100)) == (73, 74, 69)
img.save('test.jpg', 'JPEG') |
I now tried https://github.com/bigcat88/pillow_heif/ which works well |
I see there are plenty of alternatives and forks. Nevertheless, I want to present my own solution. First of all, this is not just a fork with some fixes, I've found people who can be responsible for pyheif development. There are already three versions released after reincarnation. So changes more fundamental, like lazy loading or better pypy memory management. So solution consist of pyheif itself, which have binary wheel for popular platforms and pure-python Pillow plugin: heif-image-pluginIt also supports AVIF for decoding. You can find more changes in Changelog section. |
this heif-image-plugin somehow crashes:
|
@Simon566 I noticed than the Issues page was disabled for https://github.com/uploadcare/heif-image-plugin. Fixed it. Please fill the report with minimal reproducible code there if you think this is heif-image-plugin problem. |
@Simon566 can you please check if the bug is reproducible with pillow-heif ? |
This works for me too, and it was easy to install. I could't get cykooz.heif compiled at all on Mac OS. |
Hi, I'm running it on Debian 11 in a python virtual environment. I'm using sigal (static image gallery generator) which uses pillow. I updated to pillow_heif and still it isn't working. How is this heif plugin correctly registered with pillow ?
I'm registering this in sigal.conf like: from PIL import Image, ImageSequence
from pillow_heif import register_heif_opener
register_heif_opener() here are my packages: regards, |
working with the testcode on console nevertheless works ...: from PIL import Image
import pillow_heif
if pillow_heif.is_supported('input.heic'):
heif_file = pillow_heif.open_heif('input.heic')
for img in heif_file: # you still can use it without iteration, like before.
img.scale(1024, 768) # `libheif` does not provide much operations, that can be done on image, so just scaling it.
heif_file.add_thumbnails([768, 512, 256]) # add three new thumbnail boxes.
# default quality is probably ~77 in x265, set it a bit lower and specify `save mask`.
heif_file.save('output.heic', quality=70, save_all=False) #save_all is True by default.
exit(0) so it looks like a issue with sigal or the way I register the plugin ( i trigger this in the sigal conf file ) |
@Simon566 Please report problems with sigal to https://github.com/saimn/sigal and with pillow-heif to https://github.com/bigcat88/pillow_heif. |
It took me super long to find this thread >_<' I have a good 100+ images that fall under this issue, all of the images have a ".jpg" extension which made finding this even more difficult. The current error that is provided for these images is here: Lines 3094 to 3096 in c4325c8
Should we at least update the code to check |
I think as long as there is no decoder, Pillow has no way of recognising that your image is HEIF. |
@mara004 good point. My mistake, I found the Image.format working after having installed the plugin. |
I've only .HEIC files generated by iPhone, but all of them contain bytes I don’t know if such research is Pillow’s job or not.
More details: strukturag/libheif#83 |
Analysing the file header for a HEIF signature might be possible. However, I don't think there's much point in adding this to Pillow. Unsupported means unsupported after all, and you don't need Pillow to determine the file type. |
This is true. I use python-magic to identify file mime type. It has been very useful. |
Pillow decodes files in two stages: It loops over the available image plugins in the loaded order, and calls the plugin’s _accept function with the first 16 bytes of the file. If the _accept function returns true, the plugin’s _open method is called to set up the image metadata and image tiles. Presence of b'ftypheic' is not a guarantee that images inside file are in x265(HEVC) encoding. Pillow already exposes get_format_mimetype function for supported files to get mimetype. |
Maybe someone will post this information in the documents, because it also took me about two days to find this topic and understand which of the HEIF plugins work for Pillow and support image encoding. |
Do you have any plans to support HEIF image format, recently introduced by apple?
The text was updated successfully, but these errors were encountered: