Skip to content

Instantly share code, notes, and snippets.

@teklynk
Created June 12, 2026 17:51
Show Gist options
  • Select an option

  • Save teklynk/1be3121af8616b7f682d791d44358429 to your computer and use it in GitHub Desktop.

Select an option

Save teklynk/1be3121af8616b7f682d791d44358429 to your computer and use it in GitHub Desktop.
Batch image file convert script
#!/usr/bin/env bash
# ------------------------------------------------------------
# batch_image_convert.sh – batch‑scale images and set 72 dpi.
# Supports: HEIC, JPG/JPEG, PNG → always saved as JPG.
#
# Options:
# -d <dir> Source directory (default: current directory)
# -s <pct> Resize percentage (e.g. 25%). Default: 50%
# ------------------------------------------------------------
set -euo pipefail # safer scripting
# ---------- Default configuration ----------
SRC_DIR="." # default = current working directory
OUT_SUBDIR="converted" # name of the output sub‑folder
DPI=72 # target DPI
DEFAULT_SCALE="50%" # fallback when -s is not given
QUALITY=85 # JPEG quality (good web default)
# ---------- Helper / usage ----------
usage() {
cat <<EOF
Usage: $(basename "$0") [-d <source_dir>] [-s <scale_pct>]
Options:
-d <source_dir> Directory containing the images to convert.
If omitted, the script uses the current directory (".").
-s <scale_pct> Resize factor, expressed as a percentage (e.g. 25%).
Must end with a percent sign. Default is 50%.
The script creates a sub‑folder named "$OUT_SUBDIR" inside the source
directory and writes the converted JPEG files there.
EOF
exit 1
}
# ---------- Parse command‑line arguments ----------
SCALE="$DEFAULT_SCALE" # initialise with default
while getopts ":d:s:h" opt; do
case "$opt" in
d) SRC_DIR="${OPTARG}" ;;
s)
# Basic validation – must contain a trailing %
if [[ "${OPTARG}" != *% ]]; then
echo "Error: -s argument must end with a % sign (e.g. 30%)." >&2
usage
fi
SCALE="${OPTARG}"
;;
h) usage ;;
\?) echo "Invalid option: -${OPTARG}" >&2; usage ;;
:) echo "Option -${OPTARG} requires an argument." >&2; usage ;;
esac
done
shift $((OPTIND-1))
# Resolve ~ and relative paths to an absolute path
SRC_DIR="$(realpath -m "$SRC_DIR")"
OUT_DIR="${SRC_DIR}/${OUT_SUBDIR}"
# ---------- Locate ImageMagick executable ----------
if command -v magick >/dev/null 2>&1; then
IM_CMD="magick"
elif command -v convert >/dev/null 2>&1; then
IM_CMD="convert"
else
echo "Error: Neither 'magick' nor 'convert' (ImageMagick) is found in your PATH."
echo "Install it with: sudo apt-get install imagemagick libheif-examples"
exit 1
fi
# ---------- Check HEIC support (once) ----------
if $IM_CMD -list format | grep -iq '^[[:space:]]*HEIC'; then
HAVE_HEIC=1
else
HAVE_HEIC=0
echo "Warning: ImageMagick on this system does NOT have HEIC support."
echo "HEIC files will be skipped. To add support, run:"
echo " sudo apt-get install libheif-dev"
echo " sudo apt-get purge imagemagick"
echo " sudo apt-get install imagemagick"
fi
# Create output directory if it doesn't exist
mkdir -p "$OUT_DIR"
# ---------- Process images ----------
shopt -s nullglob nocaseglob
for img in "$SRC_DIR"/*.{heic,jpg,jpeg,png}; do
[[ -f "$img" ]] || continue # skip non‑files
# Skip HEIC files only when we truly lack support
if [[ "${img,,}" == *.heic ]] && (( HAVE_HEIC == 0 )); then
echo "Skipping HEIC (no support): $img"
continue
fi
base_name="$(basename "${img%.*}")" # strip directory & extension
out_path="${OUT_DIR}/${base_name}.jpg"
# Run the conversion/resizing
$IM_CMD "$img" \
-resize "$SCALE" \
-density "$DPI" \
-units PixelsPerInch \
-quality "$QUALITY" \
"$out_path"
echo "Converted → $out_path"
done
shopt -u nullglob nocaseglob
echo "All done! Converted images are in '${OUT_DIR}'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment