Skip to content

Instantly share code, notes, and snippets.

@jon3laze
Last active February 17, 2023 14:28
Show Gist options
  • Select an option

  • Save jon3laze/fb8f65cba74dd1ce911c343ee1e7ad3a to your computer and use it in GitHub Desktop.

Select an option

Save jon3laze/fb8f65cba74dd1ce911c343ee1e7ad3a to your computer and use it in GitHub Desktop.
Script to normalize picture file types (convert webp and normalize jpeg/jfif)
# picnorm - normalize picture files by converting webp -> png and jpeg/jfif -> jpg
function picnorm() {
echo "picnorm - normalizing file types"
# set folder to working directory
folder=$(pwd)
# use find to list all files and extensions
extensions=$(find "$folder" -type f | awk -F '.' '{print $NF}' | sort)
# print file type count
echo "Starting count: "
echo "$extensions" | uniq -c
# check for file types to convert
# WEBP - convert webp to png using ffmpeg
if grep -q -w "webp" <<< "$extensions"; then
count=$(grep -c -w "webp" <<< "$extensions")
echo "Number of webp files: ($count)"
# loop through file and convert them to png
find "$folder" -type f -name "*.webp" | while IFS= read -r file; do
filename="${file%.*}"
ffmpeg -hide_banner -loglevel panic -y -i "$file" "$filename.png"
rm "$file"
echo "Normalizing ${file##*/} }---> ${filename##*/}.png \u2705"
done
else
echo "No webp files to convert."
fi
# JPEG - convert jpeg to jpg
if grep -q -w "jpeg" <<< "$extensions"; then
count=$(grep -c -w "jpeg" <<< "$extensions")
echo "Number of jpeg files: ($count)"
# loop through file and convert to jpg
find "$folder" -type f -name "*.jpeg" | while IFS= read -r file; do
filename="${file%.*}"
mv -f -- "$file" "$filename.jpg"
echo "Normalizing ${file##*/} }---> ${filename##*/}.png \u2705"
done
else
echo "No jpeg files to convert."
fi
# JFIF - convert jfif to jpg
if grep -q -w "jfif" <<< "$extensions"; then
count=$(grep -c -w "jfif" <<< "$extensions")
echo "Number of jfif files: ($count)"
# loop through file and convert to jpg
find "$folder" -type f -name "*.jfif" | while IFS= read -r file; do
filename="${file%.*}"
mv -f -- "$file" "$filename.jpg"
echo "Normalizing ${file##*/} }---> ${filename##*/}.png \u2705"
done
else
echo "No jfif files to convert."
fi
# refresh files and extensions list
extensions=$(find "$folder" -type f | awk -F '.' '{print $NF}' | sort)
# print file type count
echo "Final count: "
echo "$extensions" | uniq -c
# Fin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment