Last active
February 19, 2023 19:45
-
-
Save AntumDeluge/12af5ecb661c1120e9eb1027b66efc24 to your computer and use it in GitHub Desktop.
Script to Convert PNG Images to Indexed Color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## Creative Commons Zero (CC0) public domain dedication | |
# | |
# To the extent possible under law, the author has | |
# waived all copyright and related or neighboring | |
# rights to this work. This work is published from: | |
# The United States. | |
# | |
## For more information see: https://creativecommons.org/publicdomain/zero/1.0/ | |
## Script to convert PNG images to indexed color. | |
# | |
# Requires pngquant (https://pngquant.org/) & | |
## ImageMagick (https://imagemagick.org/). | |
argv=("$@") | |
argc="${#argv[@]}" | |
# check for pngquant executable | |
which pngquant > /dev/null 2>&1; res=$? | |
if [[ ${res} -gt 0 ]]; then | |
echo "ERROR: 'pngquant' executable not found. Please install pngquant: https://pngquant.org/" | |
exit 1 | |
fi | |
# check for identify executable from ImageMagick | |
which identify > /dev/null 2>&1; res=$? | |
if [[ ${res} -gt 0 ]]; then | |
echo "ERROR: 'identify' executable not found. Please install ImageMagick: https://imagemagick.org/" | |
exit 1 | |
fi | |
showUsage() { | |
echo | |
echo "$(basename $0) [-f] [-q min-max] path/to/image1 [path/to/image2...]" | |
echo | |
} | |
force=0 | |
pngq_params=("-v" "-f") | |
images=() | |
parsed_args=() | |
checkDuplicateArgs() { | |
first=$1 | |
for second in "${parsed_args[@]}"; do | |
if [[ "${first}" == "${second}" ]]; then | |
return 1 | |
fi | |
done | |
return 0 | |
} | |
args_done=0 | |
for ((idx=0; idx < ${argc}; idx++)); do | |
param="${argv[${idx}]}" | |
if [[ ${args_done} -eq 0 ]] && [[ "${param}" == -* ]]; then | |
checkDuplicateArgs "${param}"; res=$? | |
if [[ ${res} -gt 0 ]]; then | |
echo "ERROR: duplicate argument: ${param}" | |
exit 1 | |
elif [[ "${param}" == "-h" ]] || [[ "${param}" == "--help" ]]; then | |
showUsage | |
exit 0 | |
elif [[ "${param}" == "-f" ]]; then | |
force=1 | |
elif [[ "${param}" == "-q" ]]; then | |
if [[ ${argc} -le ${idx}+1 ]]; then | |
echo "ERROR: -q (quality) parameter requires a value: -q min-max" | |
showUsage | |
exit 1 | |
fi | |
idx=${idx}+1 | |
pngq_params+=("--quality" "${argv[${idx}]}") | |
fi | |
parsed_args+=("${param}") | |
else | |
# done parsing arguments | |
args_done=1 | |
images+=("${param}") | |
fi | |
done | |
imagec="${#images[@]}" | |
if [[ ${imagec} -eq 0 ]]; then | |
echo "ERROR: no images specified" | |
showUsage | |
exit 1 | |
fi | |
for img in "${images[@]}"; do | |
if [[ ! -f "${img}" ]]; then | |
echo "ERROR: file not found or taget is not a file: ${img}" | |
exit 1 | |
fi | |
mimetype=$(file --mime-type "${img}" | sed "s|^${img}: ||") | |
if [[ "${mimetype}" != "image/png" ]]; then | |
echo "ERROR: file is not a PNG image: ${img}" | |
exit 1 | |
fi | |
colors=$(identify -verbose -unique "${img}" | grep " Colors: " | sed -e 's/^.*Colors: //') | |
if [[ -z ${colors} ]]; then | |
echo -e "\nImageMagick: Error reading file ${img}" | |
exit 1 | |
fi | |
echo -e "\n${img}: found ${colors} colors" | |
if [ ${force} -eq 0 ] && [ ${colors} -gt 256 ]; then | |
echo "Image has more than 256 colors, not optimizing" | |
exit 0 | |
fi | |
pngquant "${pngq_params[@]}" -o "${img}" "${img}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment