Skip to content

Instantly share code, notes, and snippets.

@superdaigo
Created March 21, 2026 00:59
Show Gist options
  • Select an option

  • Save superdaigo/5b285f84cc5df041350c952ab3a10dee to your computer and use it in GitHub Desktop.

Select an option

Save superdaigo/5b285f84cc5df041350c952ab3a10dee to your computer and use it in GitHub Desktop.
Convert HEIC/HEIF file(s) to JPEG using ImageMagick
#!/usr/bin/env bash
# Convert HEIC/HEIF file(s) to JPEG using ImageMagick
# Usage:
# ./convert-heic-to-jpeg.sh [DIRECTORY_PATH]
set -euo pipefail
# Usage check
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <path>"
exit 2
fi
TARGET="$1"
# Verify magick is available
if ! command -v magick >/dev/null 2>&1; then
echo "Error: magick not found in PATH. Please install imagemagick"
exit 3
fi
# Normalize target path
if [ ! -e "$TARGET" ]; then
echo "Error: path does not exist: $TARGET"
exit 4
fi
# Find HEIC/HEIF files (case-insensitive) and convert each
# Extensions considered: .heic .heif
find_paths() {
if [ -d "$TARGET" ]; then
find "$TARGET" -type f \( -iname '*.heic' -o -iname '*.heif' \)
else
# Single file provided: check extension
case "${TARGET##*.}" in
heic|HEIC|heif|HEIF) printf '%s\n' "$TARGET" ;;
*) exit 0 ;;
esac
fi
}
while IFS= read -r src; do
# Determine destination filename: same dir, same base name, .jpg extension
dir=$(dirname -- "$src")
base=$(basename -- "$src")
name="${base%.*}"
dest="$dir/$name.jpg"
# If dest exists, skip with info
if [ -e "$dest" ]; then
# echo "Skipping (exists): $dest"
echo "Delete existing file: $dest"
continue
fi
# Convert using ImageMagick
echo "Converting: $src -> $dest"
if ! magick "$src" "$dest"; then
echo "Conversion failed: $src" >&2
fi
done < <(find_paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment