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

Add basic support for .heic files #519

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
dynamic = ["version"]

[project.optional-dependencies]
all = ["brotli", "feedgenerator", "zopfli", "cryptography"]
all = ["brotli", "feedgenerator", "zopfli", "cryptography", "pillow-heif"]
tests = ["pytest", "pytest-cov"]
docs = ["Sphinx>=4.1.0", "furo", "cryptography"]

Expand Down
4 changes: 2 additions & 2 deletions src/sigal/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def exif(self):
datetime_format = self.settings["datetime_format"]
return (
get_exif_tags(self.raw_exif, datetime_format=datetime_format)
if self.raw_exif and self.src_ext in (".jpg", ".jpeg")
if self.raw_exif and self.src_ext in (".jpg", ".jpeg", ".heic")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extension list here and below should also include ".heif" I guess ?
Also, those extensions should be recognized only if pillow-heif is available, so might be better to store this list somewhere where it can be imported (in the image module ?) and reused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about .heif files. As I understand it, this is more of a container format and may contain different image-formats.
I guess pillow-heif should be able to handle them all, but I have no way to test that yet.

I would actually prefer it if the program errors out if it encounters .heic images without pillow-heif installed, instead of just silently ignoring and skipping those pictures.
I mean, they are in fact Images - no matter if supported or not - and the user probably intended for them to be included in the gallery.

else None
)

Expand All @@ -289,7 +289,7 @@ def _get_markdown_metadata(self):
@cached_property
def raw_exif(self):
"""If not `None`, contains the raw EXIF tags."""
if self.src_ext in (".jpg", ".jpeg"):
if self.src_ext in (".jpg", ".jpeg", ".heic"):
return self.file_metadata["exif"]

@cached_property
Expand Down
12 changes: 10 additions & 2 deletions src/sigal/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
from pilkit.processors import Transpose
from pilkit.utils import save_image

try:
from pillow_heif import HeifImagePlugin
except:
pass

Check warning on line 50 in src/sigal/image.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/image.py#L49-L50

Added lines #L49 - L50 were not covered by tests

from . import signals, utils

# Force loading of truncated files
Expand Down Expand Up @@ -220,7 +225,10 @@

try:
with warnings.catch_warnings(record=True) as caught_warnings:
exif = img._getexif() or {}
exif = {}
exifdata = img.getexif()
if exifdata:
exif = exifdata._get_merged_dict()
except ZeroDivisionError:
logger.warning("Failed to read EXIF data.")
return None
Expand Down Expand Up @@ -290,7 +298,7 @@
logger.error("Could not open image %s metadata: %s", filename, e)
else:
try:
if os.path.splitext(filename)[1].lower() in (".jpg", ".jpeg"):
if os.path.splitext(filename)[1].lower() in (".jpg", ".jpeg", ".heic"):
exif = get_exif_data(img)
except Exception as e:
logger.warning("Could not read EXIF data from %s: %s", filename, e)
Expand Down
Binary file added tests/sample/pictures/dir1/test1/outdoor.heic
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ def test_get_exif_tags():
assert "datetime" not in simple
assert "gps" not in simple

def test_get_heic_exif_tags():
test_image = "outdoor.heic"
src_file = os.path.join(
CURRENT_DIR, "sample", "pictures", "dir1", "test1", test_image
)
data = get_exif_data(src_file)
simple = get_exif_tags(data, datetime_format="%d/%m/%Y")
assert simple["Make"] == "samsung"
assert simple["datetime"] == "01/09/2024"

def test_get_iptc_data(caplog):
test_image = "1.jpg"
Expand Down