Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

png packing put in function, use max_cores cpus #2

Merged
merged 6 commits into from Jan 16, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 42 additions & 34 deletions image_optim.sh
Expand Up @@ -5,23 +5,9 @@ echo "To kill the script, run"
echo "while true ;do ; killall optipng ; killall advpng ; killall pngcrush ; done"
echo "for a while"


VERBOSE=true
cpucores=`getconf _NPROCESSORS_ONLN`


if [[ $(git rev-parse --is-inside-work-tree) != "true" ]] >& /dev/null ; then
echo "fatal: Not a git repository!"
echo "Make sure to be in a git repo!"
echo "Exiting"
exit 2
fi

echo "WARNING, this script is supposed to be run in a git repo"
echo "We will create a new branch now which the scriptwill work in."
git branch script/image_optim
git checkout script/image_optim
echo "Done"

timestartglobal()
{
TSG=`date +%s.%N`
Expand All @@ -45,12 +31,28 @@ timeend()
TD=`calc $TE - $TS`
}


print()
{
${VERBOSE} && echo $1
}

make_sure_we_are_safe()
{
if [[ $(git rev-parse --is-inside-work-tree) != "true" ]] >& /dev/null ; then
echo "fatal: Not a git repository!"
echo "Make sure to be in a git repo!"
echo "Exiting"
exit 2
fi

echo "WARNING, this script is supposed to be run in a git repo"
echo "We will create a new branch now which the scriptwill work in."
git branch script/image_optim
git checkout script/image_optim
echo "Done"
}


jpeg_remove_comment_and_exiv()
{
print "Removing comments and exiv data from jpegs."
Expand All @@ -60,25 +62,31 @@ jpeg_remove_comment_and_exiv()
print "$TD"
}

VERBOSE=true
timestartglobal
jpeg_remove_comment_and_exiv


for i in $(git ls-files ./ | grep "\.png$"); do # png
png_optimize_all()
{
timestart
optipng -zc1-9 -zm1-9 -zs0-3 -f0-5 $i >> /tmp/image_optim_png.log
advpng -z4 $i >> /tmp/image_optim_png.log
pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time $i $i.foo >> /tmp/image_optim_png.log
#find out if we actually save some bytes or not
if [[ `du -b $i | awk '{print $1}'` -gt `du -b $i.foo | awk '{print $1}'` ]] ; then
mv $i.foo $i
else
rm $i.foo
fi
print "starting to optimize pngs"
git ls-files ./ | grep "\.png$" | xargs -P ${cpucores} -n 1 optipng -zc1-9 -zm1-9 -zs0-3 -f0-5 >> /tmp/image_optim_png.log
git ls-files ./ | grep "\.png$" | xargs -P ${cpucores} -n 1 advpng -z4 >> /tmp/image_optim_png.log
git ls-files ./ | grep "\.png$" | xargs -P ${cpucores} -n 1 -I '{}' pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time {} {}.foo >> /tmp/image_optim_png.log

# deciding for which file to use is easy for cpu,
# waiting for i/o, no need to parallelize it.
for i in $(git ls-files ./ | grep "\.png$") ; do
if [[ `du -b $i | awk '{print $1}'` -gt `du -b $i.foo | awk '{print $1}'` ]] ; then
mv $i.foo $i
else
rm $i.foo
fi
done
timeend
echo $TD $i
done &
print "optimizing pngs took $TD"
}

timestartglobal
make_sure_we_are_safe
jpeg_remove_comment_and_exiv
png_optimize_all
wait


Expand All @@ -90,7 +98,7 @@ git commit -a -m "image_optim $date"

while [ $todonr -gt 0 ] ; do
for i in $(cat /tmp/image_optim.todo | grep -e "\.jpg$" -e "\.jpeg") ; do
timestart
timestart
jpegoptim -f --strip-all $i >> /tmp/image_optim_jpeg.log
timeend
echo $TD $i
Expand Down