Skip to content

Instantly share code, notes, and snippets.

@uriel1998
Created August 19, 2023 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uriel1998/71a94220feb2b9f159ff4126839163c0 to your computer and use it in GitHub Desktop.
Save uriel1998/71a94220feb2b9f159ff4126839163c0 to your computer and use it in GitHub Desktop.
convert images between formats -- uses imagemagick and heif-convert
#!/bin/bash
# uses heif-convert: https://pypi.org/project/heif-convert/
# uses imagemagick: https://imagemagick.org/
if [ "${2}" == "" ];then
echo "We need an output format, thanks."
exit 99
fi
# Just a single file
if [ -f "${1}" ]; then
echo "Converting $1 to $2 format"
filename=$(basename -- "$1")
extension="${filename##*.}"
ext_lower=$(echo "${extension,,}")
g="${1%.*}"
if [[ "$ext_lower" = "heic" ]];then
heif-convert -f "$2" "$filename" "$g.$2"
else
convert "$filename" "$g.$2"
fi
fi
# for an entire filetype. Usage:
# convert_images heic png
# convert_images pdf jpg
# etc
for f in *.$1; do
echo "Converting $f to $2 format"
filename=$(basename -- "$f")
extension="${filename##*.}"
ext_lower=$(echo "${extension,,}")
g="${f%.*}"
if [[ "$ext_lower" = "heic" ]];then
heif-convert -f "$2" "$filename" "$g.$2"
else
convert "$filename" "$g.$2"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment