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

Do not apply EXIF orientation when saving, since EXIF is already saved #6

Open
wants to merge 5 commits into
base: libavif-plugin
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: 2 additions & 0 deletions .github/workflows/wheels-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ EOF
}

function build_libavif {
if [ -e libavif-stamp ]; then return; fi
install_rav1e
python3 -m pip install meson ninja

Expand Down Expand Up @@ -165,6 +166,7 @@ function build_libavif {
cp /usr/local/lib64/libavif.a /usr/local/lib
cp /usr/local/lib64/pkgconfig/libavif.pc /usr/local/lib/pkgconfig
fi
touch libavif-stamp
}

function build {
Expand Down
46 changes: 16 additions & 30 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from io import BytesIO
from typing import IO

from . import ExifTags, Image, ImageFile
from . import Image, ImageFile

try:
from . import _avif
Expand All @@ -24,24 +24,26 @@
def _accept(prefix: bytes) -> bool | str:
if prefix[4:8] != b"ftyp":
return False
coding_brands = (b"avif", b"avis")
container_brands = (b"mif1", b"msf1")
major_brand = prefix[8:12]
if major_brand in coding_brands:
if not SUPPORTED:
return (
"image file could not be identified because AVIF "
"support not installed"
)
return True
if major_brand in container_brands:
if major_brand in (
# coding brands
b"avif",
b"avis",
# We accept files with AVIF container brands; we can't yet know if
# the ftyp box has the correct compatible brands, but if it doesn't
# then the plugin will raise a SyntaxError which Pillow will catch
# before moving on to the next plugin that accepts the file.
#
# Also, because this file might not actually be an AVIF file, we
# don't raise an error if AVIF support isn't properly compiled.
b"mif1",
b"msf1",
):
if not SUPPORTED:
return (
"image file could not be identified because AVIF "
"support not installed"
)
return True
return False

Expand Down Expand Up @@ -104,10 +106,8 @@ def load(self) -> Image.core.PixelAccess | None:
data, timescale, tsp_in_ts, dur_in_ts = self._decoder.get_frame(
self.__frame
)
timestamp = round(1000 * (tsp_in_ts / timescale))
duration = round(1000 * (dur_in_ts / timescale))
self.info["timestamp"] = timestamp
self.info["duration"] = duration
self.info["timestamp"] = round(1000 * (tsp_in_ts / timescale))
self.info["duration"] = round(1000 * (dur_in_ts / timescale))
self.__loaded = self.__frame

# Set tile
Expand Down Expand Up @@ -164,19 +164,6 @@ def _save(
if isinstance(exif, Image.Exif):
exif = exif.tobytes()

exif_orientation = 0
if exif:
exif_data = Image.Exif()
try:
exif_data.load(exif)
except SyntaxError:
pass
else:
orientation_tag = next(
k for k, v in ExifTags.TAGS.items() if v == "Orientation"
)
exif_orientation = exif_data.get(orientation_tag) or 0

xmp = info.get("xmp", im.info.get("xmp") or im.info.get("XML:com.adobe.xmp"))

if isinstance(xmp, str):
Expand All @@ -199,7 +186,7 @@ def _save(
)
raise ValueError(msg)
advanced = tuple(
[(str(k).encode("utf-8"), str(v).encode("utf-8")) for k, v in advanced]
(str(k).encode("utf-8"), str(v).encode("utf-8")) for k, v in advanced
)

# Setup the AVIF encoder
Expand All @@ -220,7 +207,6 @@ def _save(
autotiling,
icc_profile or b"",
exif or b"",
exif_orientation,
xmp or b"",
advanced,
)
Expand Down
Loading
Loading