Projects and resources (En) > Tips and tricks
Batch converting PNG to JPG with convert from the ImageMagick suite
(1/1)
melodie:
Hi,
All is here:
http://superuser.com/questions/71028/batch-converting-png-to-jpg-in-linux
--- Citer --- 10 down vote
The convert command found on many Linux distributions is installed as part of the ImageMagick suite. Here's the bash code to run convert on all PNG files in a directory and avoid that double extension problem:
--- Code: ---for img in *.png; do
filename=${img%.*}
convert "$filename.png" "$filename.jpg"
done
--- Fin du code ---
--- Fin de citation ---
This is the one I just used, I ended with both png and jpg files in the same directory, which was the perfect result.
--- Citer ---I have a couple more solutions.
The simplest solution is like most already posted. A simple bash for loop.
--- Code: ---for i in *.png ; do convert "$i" "${i%.*}.jpg" ; done
--- Fin du code ---
For some reason I tend to avoid loops in bash so here is a more unixy xargs approach, using bash for the name-mangling.
--- Code: ---ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.*}.jpg"'
--- Fin du code ---
The one I use. It uses GNU Parallel to run multiple jobs at once, giving you a performance boost. It is installed by default on many systems and is almost definitely in your repo (it is a good program to have around).
--- Code: ---ls -1 *.png | parallel convert '{}' '{.}.jpg'
--- Fin du code ---
The number of jobs defaults to the number of processes you have. I found better CPU usage using 3 jobs on my dual-core system.
--- Code: ---ls -1 *.png | parallel -j 3 convert '{}' '{.}.jpg'
--- Fin du code ---
And if you want some stats (an ETA, jobs completed, average time per job...)
--- Code: ---ls -1 *.png | parallel --eta convert '{}' '{.}.jpg'
--- Fin du code ---
There is also an alternative syntax if you are using GNU Parallel.
--- Code: ---parallel convert '{}' '{.}.jpg' ::: *.png
--- Fin du code ---
And a similar syntax for some other versions (including debian).
--- Code: ---parallel convert '{}' '{.}.jpg' -- *.png
--- Fin du code ---
--- Fin de citation ---
More on the above linked page.
Navigation
Utiliser la version classique