Initial public release tree
This commit is contained in:
41
scripts/heic_thumbnailer.py
Normal file
41
scripts/heic_thumbnailer.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""HEIC/HEIF thumbnailer for the freedesktop (GNOME/GTK) thumbnail system.
|
||||
|
||||
Decodes a HEIC/HEIF file's SDR base image with pillow-heif (which bundles a
|
||||
modern libheif) and writes a PNG thumbnail. Used on Linux systems whose
|
||||
*system* libheif is too old to thumbnail iPhone HDR HEICs; installed under
|
||||
/usr/local by scripts/install_heic_thumbnailer.sh so it is reachable inside
|
||||
GNOME's bwrap thumbnailer sandbox (which binds /usr but not $HOME).
|
||||
|
||||
Invoked by the thumbnail system as: ... -s SIZE INPUT OUTPUT
|
||||
"""
|
||||
import sys
|
||||
|
||||
|
||||
def main(argv):
|
||||
args = argv[1:]
|
||||
size, rest = 256, []
|
||||
i = 0
|
||||
while i < len(args):
|
||||
if args[i] == "-s":
|
||||
size = int(args[i + 1]); i += 2
|
||||
else:
|
||||
rest.append(args[i]); i += 1
|
||||
if len(rest) < 2:
|
||||
sys.stderr.write("usage: heic_thumbnailer.py -s SIZE INPUT OUTPUT\n")
|
||||
return 2
|
||||
inp, outp = rest[0], rest[1]
|
||||
|
||||
import pillow_heif
|
||||
pillow_heif.register_heif_opener()
|
||||
from PIL import Image
|
||||
|
||||
im = Image.open(inp)
|
||||
im.load()
|
||||
im = im.convert("RGB")
|
||||
im.thumbnail((size, size), Image.LANCZOS)
|
||||
im.save(outp, "PNG")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv))
|
||||
72
scripts/install_heic_thumbnailer.sh
Executable file
72
scripts/install_heic_thumbnailer.sh
Executable file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# install_heic_thumbnailer.sh — enable HEIC/HEIF thumbnails in the Linux
|
||||
# (GNOME/GTK, freedesktop) file chooser and file manager.
|
||||
#
|
||||
# Why this exists: OpenCV/Qt don't thumbnail HEIC, and GNOME's thumbnail
|
||||
# system relies on the system libheif via gdk-pixbuf. On distros with an
|
||||
# old libheif (e.g. Ubuntu/Pop 22.04 ships 1.12), iPhone HDR HEICs fail to
|
||||
# thumbnail ("Metadata not correctly assigned"). This installs a small
|
||||
# thumbnailer backed by pillow-heif (which bundles a modern libheif) under
|
||||
# /usr/local — where it is reachable inside GNOME's bwrap thumbnailer
|
||||
# sandbox (the sandbox binds /usr but not $HOME, so a project venv won't do).
|
||||
#
|
||||
# Idempotent and safe to re-run. Skips entirely when not needed:
|
||||
# - non-Linux (macOS etc. thumbnail HEIC natively), or
|
||||
# - the system already thumbnails HEIC (modern libheif present).
|
||||
#
|
||||
# Needs sudo for the /usr/local install; the registration is user-local.
|
||||
#
|
||||
# ./scripts/install_heic_thumbnailer.sh
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
PREFIX=/usr/local/lib/rectify-thumbnailer
|
||||
THUMB_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/thumbnailers"
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
TEST_HEIC="$SCRIPT_DIR/../tests/images/HEIC/IMG_0338.heic"
|
||||
|
||||
# 1. Linux only — other platforms handle HEIC natively (macOS Quick Look).
|
||||
if [ "$(uname -s)" != "Linux" ]; then
|
||||
echo "Not Linux: HEIC thumbnails are handled natively here. Nothing to do."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 2. Already works natively? If the stock gdk-pixbuf thumbnailer can render
|
||||
# our committed sample HEIC, the system libheif is new enough and this
|
||||
# helper is unnecessary. (The sample lives under tests/, which is
|
||||
# export-ignored from source archives; if absent we just proceed.)
|
||||
if command -v gdk-pixbuf-thumbnailer >/dev/null 2>&1 && [ -f "$TEST_HEIC" ]; then
|
||||
tmp="$(mktemp --suffix=.png)"
|
||||
if gdk-pixbuf-thumbnailer -s 128 "$TEST_HEIC" "$tmp" >/dev/null 2>&1 \
|
||||
&& [ -s "$tmp" ]; then
|
||||
rm -f "$tmp"
|
||||
echo "System already thumbnails HEIC natively (modern libheif). Nothing to do."
|
||||
exit 0
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
fi
|
||||
|
||||
# 3. Install the pillow-heif decoder in an isolated venv under /usr/local.
|
||||
echo "Installing the pillow-heif HEIC thumbnailer under $PREFIX (sudo required)…"
|
||||
sudo install -d "$PREFIX"
|
||||
if [ ! -x "$PREFIX/venv/bin/python3" ]; then
|
||||
sudo python3 -m venv "$PREFIX/venv"
|
||||
fi
|
||||
sudo "$PREFIX/venv/bin/pip" install -q --upgrade pip pillow-heif
|
||||
sudo cp "$SCRIPT_DIR/heic_thumbnailer.py" "$PREFIX/thumbnailer.py"
|
||||
|
||||
# 4. Register with the freedesktop thumbnail system (user-local, no sudo).
|
||||
mkdir -p "$THUMB_DIR"
|
||||
cat > "$THUMB_DIR/rectify-heic.thumbnailer" <<EOF
|
||||
[Thumbnailer Entry]
|
||||
TryExec=$PREFIX/venv/bin/python3
|
||||
Exec=$PREFIX/venv/bin/python3 $PREFIX/thumbnailer.py -s %s %i %o
|
||||
MimeType=image/heif;image/heic;
|
||||
EOF
|
||||
|
||||
# 5. Drop cached thumbnail failures so HEICs are retried.
|
||||
rm -rf "${XDG_CACHE_HOME:-$HOME/.cache}/thumbnails/fail" 2>/dev/null || true
|
||||
|
||||
echo "Done. HEIC thumbnails will appear in the file chooser and file manager."
|
||||
echo "Restart the file manager to pick it up immediately: nautilus -q"
|
||||
Reference in New Issue
Block a user