LINUX.ORG.RU

История изменений

Исправление peregrine, (текущая версия) :

#!/bin/bash
#Функции обработки
opt_optipng ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(optipng):\e[00m \e[01;32m$input\e[00m"
	optipng0.7.4 -o3 "$input" >/dev/null 2>&1
}

opt_advpng ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(advpng):\e[00m \e[01;32m$input\e[00m"
	advpng -z -4 "$input">/dev/null 2>&1
}

opt_advdef ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(advdef):\e[00m \e[01;32m$input\e[00m"
	advdef -z -4 "$input">/dev/null 2>&1
}

opt_jpegtran ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(jpegtran):\e[00m \e[01;32m$input\e[00m"
	jpegtran -optimize -progressive -copy none -outfile "$input" "$input" 2>&1
	#jpegtran -arithmetic -copy none -outfile "$input" "$input" 2>&1
}

opt_gifsicle ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(gifsicle):\e[00m \e[01;32m$input\e[00m"
	gifsicle --batch --optimize=3 "$input" >/dev/null 2>&1
}
#Очистка экрана и поиск файлов для обработки
clear
SIZEPNG=$(find . -type f -iname '*.png' -printf '%s\n' | tee >(numpng=$(wc -l)) | awk '{SUM+=$1} END {print SUM}');
SIZEGIF=$(find . -type f -iname '*.gif' -printf '%s\n' | tee >(numgif=$(wc -l)) | awk '{SUM+=$1} END {print SUM}');
SIZEJPG=$(find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -printf '%s\n' | tee >(numjpg=$(wc -l)) | awk '{SUM+=$1} END {print SUM}')
SIZE=$[SIZEPNG+SIZEGIF+SIZEJPG]

#Рабочая часть скрипта
export -f opt_optipng
find -type f -iname '*.png' | sort | parallel --no-notice opt_optipng '{}'
export -f opt_advpng
find -type f -iname '*.png' | sort | parallel --no-notice opt_advpng '{}'
export -f opt_advdef
find -type f -iname '*.png' | sort | parallel --no-notice opt_advdef '{}'
export -f opt_jpegtran
find -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) | sort | parallel --no-notice opt_jpegtran '{}'
export -f opt_gifsicle
find -type f -iname '*.gif' | sort | parallel --no-notice opt_gifsicle '{}'

#Оформление информации для вывода
echo -e "\e[01;33mГотово!\e[00m"
SIZEPNG2=$(find . -type f -iname '*.png' -printf '%s\n' | awk '{SUM+=$1} END {print SUM}');
SIZEGIF2=$(find . -type f -iname '*.gif' -printf '%s\n' | awk '{SUM+=$1} END {print SUM}');
SIZEJPG2=$(find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -printf '%s\n' | awk '{SUM+=$1} END {print SUM}')
SIZE2=$[SIZEPNG2+SIZEGIF2+SIZEJPG2]
if [ $SIZEPNG ]; then
	PERCENTPNG=$[100-SIZEPNG2*100/SIZEPNG]
fi
if [ $SIZEGIF ]; then
	PERCENTGIF=$[100-SIZEGIF2*100/SIZEGIF]
fi
if [ $SIZEJPG ]; then
	PERCENTJPG=$[100-SIZEJPG2*100/SIZEJPG]
fi
if [ $SIZE ]; then
	PERCENT=$[100-SIZE2*100/SIZE]
	echo -e "Исходный размер всех обрабатываемых файлов был \e[01;31m$SIZE\e[00m байт а после обработки составил \e[01;31m$SIZE2\e[00m байт. Среднее сжатие на \e[01;31m$PERCENT%\e[00m
Из них:"
fi
if [ $SIZEPNG ]; then
	echo -e "PNG: было: \e[01;31m$SIZEPNG\e[00m байт, стало \e[01;31m$SIZEPNG2\e[00m байт, сжалось на \e[01;31m$PERCENTPNG%\e[00m"
fi
if [ $SIZEGIF ]; then
	echo -e "GIF: было: \e[01;31m$SIZEGIF\e[00m байт, стало \e[01;31m$SIZEGIF2\e[00m байт, сжалось на \e[01;31m$PERCENTGIF%\e[00m"
fi
if [ $SIZEJPG ]; then
	echo -e "JPG: было: \e[01;31m$SIZEJPG\e[00m байт, стало \e[01;31m$SIZEJPG2\e[00m байт, сжалось на \e[01;31m$PERCENTJPG%\e[00m"
fi

Всё уже написано, задолго до вас. Выложил свой велосипед. Не забудтье команды поменять на нужные, тут optipng отдельный используется. Ну и этот скрипт для оптимизации без потерь качества картинки. Можно с потерями более эффективно жать.

Исходная версия peregrine, :

#!/bin/bash
#Функции обработки
opt_optipng ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(optipng):\e[00m \e[01;32m$input\e[00m"
	optipng0.7.4 -o3 "$input" >/dev/null 2>&1
}

opt_advpng ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(advpng):\e[00m \e[01;32m$input\e[00m"
	advpng -z -4 "$input">/dev/null 2>&1
}

opt_advdef ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(advdef):\e[00m \e[01;32m$input\e[00m"
	advdef -z -4 "$input">/dev/null 2>&1
}

opt_jpegtran ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(jpegtran):\e[00m \e[01;32m$input\e[00m"
	jpegtran -optimize -progressive -copy none -outfile "$input" "$input" 2>&1
	#jpegtran -arithmetic -copy none -outfile "$input" "$input" 2>&1
}

opt_gifsicle ()
{
	input="$1"
	echo -e "\e[01;34mОбрабатывается\e[00m \e[01;31m(gifsicle):\e[00m \e[01;32m$input\e[00m"
	gifsicle --batch --optimize=3 "$input" >/dev/null 2>&1
}
#Очистка экрана и поиск файлов для обработки
clear
SIZEPNG=$(find . -type f -iname '*.png' -printf '%s\n' | tee >(numpng=$(wc -l)) | awk '{SUM+=$1} END {print SUM}');
SIZEGIF=$(find . -type f -iname '*.gif' -printf '%s\n' | tee >(numgif=$(wc -l)) | awk '{SUM+=$1} END {print SUM}');
SIZEJPG=$(find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -printf '%s\n' | tee >(numjpg=$(wc -l)) | awk '{SUM+=$1} END {print SUM}')
SIZE=$[SIZEPNG+SIZEGIF+SIZEJPG]

#Рабочая часть скрипта
export -f opt_optipng
find -type f -iname '*.png' | sort | parallel --no-notice opt_optipng '{}'
export -f opt_advpng
find -type f -iname '*.png' | sort | parallel --no-notice opt_advpng '{}'
export -f opt_advdef
find -type f -iname '*.png' | sort | parallel --no-notice opt_advdef '{}'
export -f opt_jpegtran
find -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) | sort | parallel --no-notice opt_jpegtran '{}'
export -f opt_gifsicle
find -type f -iname '*.gif' | sort | parallel --no-notice opt_gifsicle '{}'

#Оформление информации для вывода
echo -e "\e[01;33mГотово!\e[00m"
SIZEPNG2=$(find . -type f -iname '*.png' -printf '%s\n' | awk '{SUM+=$1} END {print SUM}');
SIZEGIF2=$(find . -type f -iname '*.gif' -printf '%s\n' | awk '{SUM+=$1} END {print SUM}');
SIZEJPG2=$(find . -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -printf '%s\n' | awk '{SUM+=$1} END {print SUM}')
SIZE2=$[SIZEPNG2+SIZEGIF2+SIZEJPG2]
if [ $SIZEPNG ]; then
	PERCENTPNG=$[100-SIZEPNG2*100/SIZEPNG]
fi
if [ $SIZEGIF ]; then
	PERCENTGIF=$[100-SIZEGIF2*100/SIZEGIF]
fi
if [ $SIZEJPG ]; then
	PERCENTJPG=$[100-SIZEJPG2*100/SIZEJPG]
fi
if [ $SIZE ]; then
	PERCENT=$[100-SIZE2*100/SIZE]
	echo -e "Исходный размер всех обрабатываемых файлов был \e[01;31m$SIZE\e[00m байт а после обработки составил \e[01;31m$SIZE2\e[00m байт. Среднее сжатие на \e[01;31m$PERCENT%\e[00m
Из них:"
fi
if [ $SIZEPNG ]; then
	echo -e "PNG: было: \e[01;31m$SIZEPNG\e[00m байт, стало \e[01;31m$SIZEPNG2\e[00m байт, сжалось на \e[01;31m$PERCENTPNG%\e[00m"
fi
if [ $SIZEGIF ]; then
	echo -e "GIF: было: \e[01;31m$SIZEGIF\e[00m байт, стало \e[01;31m$SIZEGIF2\e[00m байт, сжалось на \e[01;31m$PERCENTGIF%\e[00m"
fi
if [ $SIZEJPG ]; then
	echo -e "JPG: было: \e[01;31m$SIZEJPG\e[00m байт, стало \e[01;31m$SIZEJPG2\e[00m байт, сжалось на \e[01;31m$PERCENTJPG%\e[00m"
fi

Всё уже написано, задолго до вас. Выложил свой велосипед.