Skip to content

Instantly share code, notes, and snippets.

@igor-kamil
Last active September 20, 2023 17:23
Show Gist options
  • Save igor-kamil/5020f57b32c14b202042b349670753b1 to your computer and use it in GitHub Desktop.
Save igor-kamil/5020f57b32c14b202042b349670753b1 to your computer and use it in GitHub Desktop.
script to convert all TIFF images in the source folder into JPEG
#!/bin/bash
# Check if the user provided a folder path as an argument - otherwise use current
if [ $# -eq 1 ]; then
source_folder="$1"
else
source_folder="."
fi
# Check if the source folder exists
if [ ! -d "$source_folder" ]; then
echo "Source folder not found: $source_folder"
exit 1
fi
destination_folder="$source_folder/converted_images"
# Check if the destination folder exists; if not, create it
if [ ! -d "$destination_folder" ]; then
mkdir -p "$destination_folder"
fi
# Create a separate folder for the processed JPEGs
processed_folder="$source_folder/processed_images"
mkdir -p "$processed_folder"
for tiff_file in "$source_folder"/*LP_A4.tif; do
if [ -f "$tiff_file" ]; then
base_name=$(basename "$tiff_file" .tif)
# Convert TIFF to JPEG using convert and save it in the processed folder
convert "$tiff_file" -colorspace sRGB "$processed_folder/$base_name.jpg"
if [ -f "$processed_folder/$base_name.jpg" ]; then
# echo "Converted $tiff_file to JPEG in processed folder: $processed_folder/$base_name.jpg"
printf "."
# Apply ICC profile with jpgicc to the processed JPEG
jpgicc "$processed_folder/$base_name.jpg" "$destination_folder/$base_name.jpg" > /dev/null 2>&1
# Optimize the processed JPEG using jpegoptim
jpegoptim --max=85 --strip-all "$destination_folder/$base_name.jpg" > /dev/null 2>&1
else
echo "\nConversion of $tiff_file to JPEG failed."
fi
fi
done
# Remove the processed folder
rm -rf "$processed_folder"
echo "\nConversion complete."
@igor-kamil
Copy link
Author

usage: chmod +x convert_images.sh
convert_images.sh {source_dir}

@igor-kamil
Copy link
Author

requirements:
ImageMagick for convert
LittleCMS for jpgicc
JPEGoptim

you can use homebrew to install them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment