Optimizing catalog images using multiple processor cores

I have several PNG images in the catalog, and I use optipngto optimize and reduce the size of the image. The problem is that it takes too much time to optimize all files.

I have a quad-core processor, and I noticed that it optipnguses only one core when I optimize the directory.

This is the code I'm using:

ls -1 | while read line
do 
    optipng -o7 "$line"
done

Is it possible to execute optipngfor four different files in parallel while reading a directory?

+3
source share
3 answers

There is another solution, including xargs.

find some/dir/ -iname '*.png' -print0 | xargs -0 -n 1 -P 4 optipng -o7

-P 4 4 , -n 1 .

, , , :

find some/dir/ -iname '*.png' | sort | xargs -d \\n -n 1 -P 4 optipng -o7

Joe Lencioni .


: script zopflipng ( , optipng) : zopflipng_in_place

+8

optipng , , n, . n=4 , 4 :

n=0
for image in *
 do
  optipng -o7 "$image" &
  n=$(( $n + 1 ))
  [ "$n" -eq 4 ] && n=0 && wait
done

bg ( n low), . , * for image in *.

+4

I use optipng with GNU parallel (included with every Linux distribution):

parallel --bar 'optipng {}' ::: file1.png file2.png morefile*.png

Advantage: you have a dashboard showing progress.

+1
source

All Articles