Last active
August 8, 2019 21:12
-
-
Save gangelo/480746bfc35dd3d43d2bd49bb28f2cc6 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
# This script converts .heic files to .jpg files. The actual file encoding | |
# is checked, so that if the file is renamed correctly, it will be named | |
# and converted properly. | |
# | |
# Instructions: | |
# | |
# From the folder that contains the image files you want to convert... | |
# | |
# Reads all files in the current folder and renames them to lowercase. | |
# | |
# And also... | |
# | |
# Checks the encoding, and if the encoding is image/heic and file | |
# extension is NOT .heic, the file name is given a .heic extension. | |
# | |
# Then... | |
# | |
# Checks the encoding, and if the encoding is image/jpeg and file | |
# extension is NOT .jpg, the file name is given a .jpg extension. | |
# | |
# Then... | |
# | |
# All .heic files are converted to .jpg image files. | |
# | |
# Only files with encoding of image/heic and image/jpeg are processed, | |
# all other files are ignored. | |
# | |
# The --live switch will actually change files. | |
# $ ~/bin/mvimage.sh [--live] | |
live=0 | |
if [[ $1 == --live ]]; then | |
live=1 | |
fi | |
if [[ $live -eq 1 ]]; then | |
echo Running in live mode... | |
else | |
echo Running in pretend mode... | |
fi | |
shopt -s nocasematch | |
for file in *.*; do | |
new_filename=$file | |
# Check to see if filename has spaces... | |
pattern=" |'" | |
if [[ $file =~ $pattern ]]; then | |
# Remove spaces from filenames... | |
new_filename="${file// /_}" | |
new_filename=$(echo "$new_filename" | tr '[:upper:]' '[:lower:]') | |
while [[ -f "$new_filename" ]]; do | |
echo "$new_filename exists, trying _$new_filename..." | |
new_filename="_$new_filename" | |
done | |
fi | |
# Make the filename lowercase and rename the file... | |
new_filename=$(echo "$new_filename" | tr '[:upper:]' '[:lower:]') | |
# Extract file encoding information... | |
file_encoding_info=$( file -I $new_filename ) | |
# Split the filename so we can use the filename and check the extension | |
# against the file encoding... | |
filename=$(basename -- "$new_filename") | |
extension="${filename##*.}" | |
extension="$(echo $extension | tr '[:upper:]' '[:lower:]')" | |
filename="${filename%.*}" | |
IFS=' ' read -r -a encoding_array <<< "$file_encoding_info" | |
file_encoding="${encoding_array[1]}" | |
# Rename the file | |
if [[ ! $file_encoding == "image/jpeg;" ]] && [[ ! $file_encoding == "image/heic;" ]]; then | |
echo "Ignoring file $file -> encoding is not image/jpeg or image/heic." | |
else | |
if [[ $live -eq 1 ]]; then | |
echo "mv $file $new_filename" | |
mv "$file" $new_filename | |
else | |
echo "Pretend -> mv $file $new_filename" | |
fi | |
fi | |
if [[ $extension != ".heic" ]] && [[ $file_encoding == "image/heic;" ]]; then | |
heic_filename="$filename.heic" | |
if [[ $live -eq 1 ]]; then | |
echo "mv $file $heic_filename" | |
mv $file $heic_filename | |
else | |
echo "Pretend -> mv $file $heic_filename" | |
fi | |
fi | |
if [[ $file_encoding == "image/jpeg;" ]] && [[ $extension != ".heic" ]] && [ $extension != "jpg" ]; then | |
if [[ $live -eq 1 ]]; then | |
echo "mv $file $filename.jpg" | |
mv $file "$filename.jpg" | |
else | |
echo "Pretend -> mv $file $filename.jpg" | |
fi | |
fi | |
done | |
if [[ $live -eq 1 ]]; then | |
mogrify -format jpg *.heic && rm *.heic | |
else | |
echo "Pretend -> mogrify -format jpg *.heic && rm *.heic" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment