Skip to content

Instantly share code, notes, and snippets.

@caubry
Last active December 13, 2023 14:50
Show Gist options
  • Select an option

  • Save caubry/7103438 to your computer and use it in GitHub Desktop.

Select an option

Save caubry/7103438 to your computer and use it in GitHub Desktop.
Bash script using pngquant and jpegoptim library to compress PNG, JPG and JPEG, with batch compression as an option.
#!bin/bash
pwd=`pwd`
echo -n "Please enter the file path: ";
read filePath;
if [ ! -f "$filePath" ]
then
echo "File "$filePath" not found!";
exit;
fi
imageType="${filePath##*.}";
lengthPath="${#filePath}";
originalName="${filePath%.*}";
originalDuSizeFile=$(du -h "$filePath" | awk '{print $1}');
originalLsSize=$(ls -l "$filePath" | awk '{print $5}');
originalDuSize=${originalDuSizeFile%?};
pause()
{
read -p "$*";
}
install_pngquant()
{
cd ~/Documents/;
git clone git://github.com/pornel/pngquant.git;
if [[ -d 'pngquant' ]]
then
cd pngquant;
make -s;
make -s install;
cd ..;
rm -rf ~/Documents/pngquant;
command -v pngquant >/dev/null 2>&1 ||
{
echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻";
exit;
}
echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
pause 'Press [Enter] key to compress your image...';
run_pngquant;
else
echo "Couldn't clone pngquant git repo. \nDo you have an Internet access?";
exit;
fi
}
run_pngquant()
{
command -v pngquant >/dev/null 2>&1 ||
{
echo -e >&2 "Pngquant command line tool hasn't been installed.";
while true; do
read -p "Do you wish to install this program (Y/N)? " yn
case $yn in
[Yy]* ) install_pngquant;;
[Nn]* ) echo "End"; exit;;
* ) echo "Please answer Yes or No.";;
esac
done
exit;
}
pngquant -vf "$filePath" --ext "$fileExtension";
imageOutput="${originalName}$fileExtension";
output=$(du -h "$imageOutput");
outputSizeFile=$(du -h "$imageOutput" | awk '{print $1}');
outputSize=${outputSizeFile%?};
if [[ $outputSize -ge $originalDuSize ]]
then
echo -e "\n******************************************************************************** \n********** The compression has already been performed on this file. ************ \n******************************************************************************** \n";
exit;
else
echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: $output \n";
exit;
fi
}
install_libjpeg()
{
cd /usr/local/include/;
if [[ -f 'jpeglib.h' ]]
then
install_jpegoptim;
else
cd ~/Documents/;
curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
tar -xf jpegsrc.v8c.tar.gz
rm jpegsrc.v8c.tar.gz
if [[ -d 'jpeg-8c' ]]
then
cd jpeg-8c/;
./configure;
make;
make -s install;
cd ..;
rm -rf jpeg-8c/;
pause 'Press [Enter] key to install jpegoptim...';
install_jpegoptim;
else
echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻ \nDo you have Internet access?";
exit;
fi
fi
}
install_jpegoptim()
{
cd ~/Documents/;
git clone https://github.com/tjko/jpegoptim.git;
if [[ -d 'jpegoptim' ]]
then
cd jpegoptim;
./configure;
make -s;
make -s install;
cd ..;
rm -rf ~/Documents/jpegoptim;
command -v jpegoptim >/dev/null 2>&1 ||
{
echo "Something went horribly wrong and jpegoptim hasn't been installed! (╯°□°)╯︵ ┻━┻";
exit;
}
echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
pause 'Press [Enter] key to compress your image...';
run_jpegoptim;
else
echo -e "Couldn't get jpegoptim. \nDo you have Internet access?";
exit;
fi
}
run_jpegoptim_twenty_kb()
{
make_image_copy;
twenty_kb=20000;
copy_ext="_copy";
copyTempFilePath="${tempFilePath}$copy_ext";
cp $tempFilePath $copyTempFilePath;
while [ $twenty_kb -le $outputSize -a $qualityCompression -gt 20 ]
do
cp $copyTempFilePath $tempFilePath;
qualityCompression=$((qualityCompression-1));
jpegoptim -v --max=$qualityCompression "$tempFilePath";
outputSize=$(ls -l "$tempFilePath" | awk '{print $5}');
done
if $overwrite
then
extension=".$imageType";
else
extension=$fileExtension;
fi
rm $copyTempFilePath;
mv $tempFilePath ${originalName}$extension;
fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');
if [[ "$fileOutput" -ge "$originalLsSize" ]]
then
echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n******************************************************************************** \n";
remove_temp_folder;
exit;
else
if [[ $twenty_kb -le $outputSize ]]; then
echo -e "\n******************************************************************************** \n*********************** Compression has been performed! ************************ \n******************************************************************************** \nThe minimum quality factor has been set to 20 to avoid the image to be corrupted \n*********** Note that the size of the new file is superior as 20Kb! ************ \n******************************************************************************** \n\n\xF0\x9f\x8d\xba Output: ${originalName}$extension, $outputSize \n";
else
echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: ${originalName}$extension, $outputSize \n";
fi
remove_temp_folder;
exit;
fi
}
run_jpegoptim_quality()
{
if $overwrite
then
currentFile=$filePath;
extension=".$imageType";
else
make_image_copy;
currentFile=$tempFilePath;
extension=$fileExtension;
fi
jpegoptim -v --max=$qualityCompression "$currentFile";
mv $currentFile ${originalName}$extension;
fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');
if [[ "$fileOutput" -ge "$originalLsSize" ]]
then
echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n******************************************************************************** \n";
remove_temp_folder;
case $overwrite in
(false) rm ${originalName}$extension;;
esac
exit;
else
echo -e "\n\xF0\x9f\x8d\xba Compression done! \n";
remove_temp_folder;
exit;
fi
}
make_image_copy()
{
temp="temp/";
mkdir ${pwd}$temp;
tempName="_temp";
tempDir=${pwd}$temp;
cp $filePath ${tempDir}$tempName;
tempFilePath=${tempDir}$tempName;
}
remove_temp_folder()
{
rm -rf ${pwd}$temp;
}
run_jpegoptim_command()
{
while true; do
read -p "Do you wish to overwrite your image (Y/N)? " yn
case $yn in
[Yy]* ) overwrite=true; break;;
[Nn]* ) overwrite=false; break;;
* ) echo "Please answer Yes or No.";;
esac
done
if $once
then
while true; do
read -p "Please enter the maximum image quality factor [0-100]: " qualityCompression;
case $qualityCompression in
[0-99]*) run_jpegoptim_quality;;
* ) echo "$qualityCompression, Not a number between 0 and 100.";;
esac
done
else
qualityCompression=80;
jpegoptim -v --max=$qualityCompression "$filePath";
outputSize=$(ls -l "$filePath" | awk '{print $5}');
run_jpegoptim_twenty_kb;
fi
}
run_jpegoptim()
{
command -v jpegoptim >/dev/null 2>&1 ||
{
echo -e >&2 "Jpegoptim hasn't been installed.";
while true; do
read -p "Do you wish to install this program (Y/N)? " yn
case $yn in
[Yy]* ) install_libjpeg;;
[Nn]* ) echo "End"; exit;;
* ) echo "Please answer Yes or No.";;
esac
done
exit;
}
while true; do
read -p "Do you need this image to be under 20kb (Y/N): " yn;
case $yn in
[Yy]* ) once=false; run_jpegoptim_command $once; break;;
[Nn]* ) once=true; run_jpegoptim_command $once; break;;
* ) echo "Please answer Yes or No.";;
esac
done
}
customExt="_MINI";
pngExt="png";
jpgExt="jpg";
jpegExt="jpeg";
case ${imageType:(-1)} in
[A-Z]*) upperLetter=true;;
[a-z]*) upperLetter=false;;
esac
if $upperLetter
then
fileExtension="${customExt}".$imageType"";
lowerCaseLetters=$(echo $imageType | awk '{print tolower($0)}');
imageType="$lowerCaseLetters";
else
imageType="$imageType";
fileExtension="${customExt}".$imageType"";
fi
if [[ "$imageType" == *$pngExt* ]]
then
run_pngquant;
exit;
elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
then
run_jpegoptim;
exit;
else
echo "Compression works with PNG, JPG and JPEG only.";
exit;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment