liuyenting
9/18/2016 - 1:01 AM

Batch image format conversion using SIPS (Scriptable Image Processing System) in OS X.

Batch image format conversion using SIPS (Scriptable Image Processing System) in OS X.

#!/bin/bash

echo ""
read -e -p "Target folder name: " folder

echo "Duplicate folder structure of $folder.."
if [ -d "Compressed-${folder}" ]; then
	echo ""
	read -r -p "\"Compressed-${folder}\" exist, overwrite? [y/n] " response
	if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]] 
	then
		rm -rf ./Compressed-${folder}
	else
		echo "Program terminated.."
		echo ""		
		exit
	fi
fi
mkdir ./Compressed-${folder}
cd ./Compressed-${folder}
(cd ../${folder}; find . -depth -type d ! -name . -print0) | xargs -0 mkdir -p

cd ..
echo ""
read -p "Target file extension (jpg,jpeg,tiff...): " extension
echo "Target file extension is \"$extension\".."

echo ""
read -e -p "Set the compression ratio (1-100%): " ratio

original="./${folder}"
replaced="./Compressed-${folder}"

find ./${folder} -iname "*.$extension" | while read file; do
	echo "Processing $file"
	newpath="${file/$original/$replaced}"
#	sips -s format tiff -s formatOptions $ratio -s formatOptions lzw "$file" --out "${newpath}" &> /dev/null
	sips -s format jpeg -s formatOptions $ratio "$file" --out "${newpath}" &> /dev/null
done

echo "\nProcess complete..\n"