niol / lazygal

Static web gallery generator
https://sml.zincube.net/~niol/repositories.git/lazygal/about/
GNU General Public License v2.0
6 stars 3 forks source link

Support for HEIF/HEIC images #11

Open thomasdn opened 3 years ago

thomasdn commented 3 years ago

Lazygal does not seem to support HEIF images. HEIF Newer Apple devices such as iPhone or iPads take photos in the HEIF format[1]. These photos are using the .heic extension. Lazygal does not seem to include the .heic files in the generated gallery.

Lazygal seems to use the Pillow module for image manipulation. Pillow seems to support the HEIF format[2], so I see no reason that lazygal shouldn't automatically include .heic images present in the source folder.

The pyheif package seems to enable HEIF support with Pillow.

thomasdn commented 3 years ago

pyhief-pillow-opener https://pypi.org/project/pyheif-pillow-opener/ seems like a fairly easy way.

See this example:

from PIL import Image
from pyheif_pillow_opener import register_heif_opener
register_heif_opener()
image = Image.open('test.heic')
image.load()
niol commented 3 years ago

Thanks for bringing this to my attention.

Steps to add this to lazygal: 1) add a requirements.txt to register lazygal dependencies (I use Debian and have not managed to do this before) and the new dependency pyheif-pillow-opener 2) add processing of .heif files 3) sort out how to get metadata from those files and hope GExiv2 can do it 4) add test cases for all of this

thomasdn commented 3 years ago

I started on this. I made a requirements.txt and a Makefile to use it to build a venv.

requirements.txt:

pyheif-pillow-opener
genshi
Pillow
GLib

Makefile:

# Makefile

venv: venv/bin/activate

venv/bin/activate: requirements.txt
        test -d venv || python3 -m venv venv
        . venv/bin/activate
        venv/bin/pip3 install wheel
        venv/bin/pip3 install -Ur requirements.txt
        touch venv/bin/activate

cleanvenv:
        rm -rf venv

Some dependencies I could not find in PyPI so could not put in requirements.txt. I installed the following packages using apt (not sure of all of them are needed though):

build-essential
devscripts
gettext
liblocale-msgfmt-perl
intltool
intltool-debian
ffmpeg
pandoc
libgexiv2-2
libgexiv2-dev
exiv2

Then I make the venv, activate it. Then run ./setup.py test and it fails missing some 'gi' module. I have not been able to locate this module and thus cannot continue.

(venv) (sigal@bullseye-debootstrap) (2021-07-16 09:47:33) [0]
~/git/lazygal$ ./setup.py test
running test
Traceback (most recent call last):
  File "/home/sigal/git/lazygal/./setup.py", line 279, in <module>
    setup(
  File "/usr/lib/python3.9/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.9/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.9/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/sigal/git/lazygal/./setup.py", line 55, in run
    import lazygaltest
  File "/home/sigal/git/lazygal/lazygaltest/__init__.py", line 25, in <module>
    from lazygal.sourcetree import Directory
  File "/home/sigal/git/lazygal/lazygal/sourcetree.py", line 33, in <module>
    from . import pathutils, make, metadata
  File "/home/sigal/git/lazygal/lazygal/metadata.py", line 26, in <module>
    import gi
ModuleNotFoundError: No module named 'gi'

However, I did make a patch that might enable support of HEIF/HEIC images using the pyheif_pillow_opener. But, alas, it has not been tested:

diff --git a/lazygal/eyecandy.py b/lazygal/eyecandy.py
index 2693888..0740dd9 100644
--- a/lazygal/eyecandy.py
+++ b/lazygal/eyecandy.py
@@ -18,6 +18,10 @@

 import random
 from PIL import Image, ImageChops, ImageFilter
+from pyheif_pillow_opener import register_heif_opener
+
+register_heif_opener()
+

 class Color:
diff --git a/lazygal/genmedia.py b/lazygal/genmedia.py
index e75eb3a..4b06f40 100644
--- a/lazygal/genmedia.py
+++ b/lazygal/genmedia.py
@@ -23,6 +23,10 @@ from PIL import Image as PILImage
 # lazygal has her own ImageFile class, so avoid trouble
 from PIL import ImageFile as PILImageFile
 PILImageFile.MAXBLOCK = 1024 * 1024  # default is 64k, not enough for big pics
+from pyheif_pillow_opener import register_heif_opener
+
+register_heif_opener()
+

 from . import make
 from . import genfile
diff --git a/lazygal/mediautils.py b/lazygal/mediautils.py
index c2f8ded..f988417 100644
--- a/lazygal/mediautils.py
+++ b/lazygal/mediautils.py
@@ -26,6 +26,10 @@ import tempfile

 from PIL import Image as PILImage
+from pyheif_pillow_opener import register_heif_opener
+
+register_heif_opener()
+

 class VideoError(Exception): pass
diff --git a/lazygal/sourcetree.py b/lazygal/sourcetree.py
index 4e366bc..f65ec16 100644
--- a/lazygal/sourcetree.py
+++ b/lazygal/sourcetree.py
@@ -25,6 +25,10 @@ import re
 import time

 from PIL import Image as PILImage
+from pyheif_pillow_opener import register_heif_opener
+
+register_heif_opener()
+

 from . import pathutils, make, metadata
 from . import mediautils

There will likely have to be done a bit of work in getting file name references correct. For example when reading a source file as filename.heic. Then I assume the thumbnail will be saved as jpg and thus the filename extension in the destination must be .jpg as well.

Of course, I have not done any work on bullets 3 and 4 in your list regarding metadata and test cases.

Sorry for not being able to contribute a fully working solution. I hope this is at least somewhat valuable.

niol commented 3 years ago

Thanks a lot for this work.

For module gi you need PyGObject. The other deps are for building manpages and translations. I think register_heif_opener() will be required only once. You'll also need to make lazygal process .heif files in lazygal/sourcetree.py class MediaHandler.

thomasdn commented 3 years ago

I added PyGObject to requirements.txt and finally made the venv work.

When I run setup.py test, I get a whole bunch of errors: https://paste.yt/p15612.html

I have not real idea how to solve these. I guess I am in a bit over my head here.

niol commented 3 years ago

Your venv misses gi overrides (file /usr/lib/python3/dist-packages/gi/overrides/GExiv2.py). I'm not familiar with venv and do not know how to fix this.

thomasdn commented 11 months ago

Would it be possible to use the patch I sent above? Not sure what else is needed or how to contribute what is needed?

niol commented 11 months ago

Please submit a working pull request. Only one call to register_heif_opener() should be required. Also please make this optional by catching ImportError.