Files
rectify/scripts/install_heic_thumbnailer.sh

73 lines
3.0 KiB
Bash
Raw Permalink Normal View History

2026-08-04 18:31:51 +02:00
#!/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"