Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

png packing put in function, use max_cores cpus #2

Merged
merged 6 commits into from
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 34 deletions.
  1. +42 −34 image_optim.sh
View
76 image_optim.sh
@@ -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`
@@ -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."
@@ -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
@@ -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
Something went wrong with that request. Please try again.