Batch converting PNG to JPG in linux
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dream Voyager Looping
--
Chapters
00:00 Question
00:29 Accepted answer (Score 343)
01:10 Answer 2 (Score 120)
02:48 Answer 3 (Score 29)
03:14 Answer 4 (Score 17)
06:56 Thank you
--
Full question
https://superuser.com/questions/71028/ba...
Accepted answer links:
[ImageMagick]: https://www.imagemagick.org/script/index...
[Mogrify]: https://www.imagemagick.org/script/mogri...
Answer 4 links:
[mogrify]: http://www.imagemagick.org/script/mogrif...
[ImageMagick]: http://www.imagemagick.org/script/index....
[the site]: http://www.imagemagick.org/script/mogrif...
[- format]: http://www.imagemagick.org/script/comman...
[short circuit evaluation]: https://en.wikipedia.org/wiki/Short-circ...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #shellscript #jpeg #png
#avk47
ACCEPTED ANSWER
Score 366
Your best bet would be to use ImageMagick.
I am not an expert in the actual usage, but I know you can pretty much do anything image-related with this!
An example is:
convert image.png image.jpg
which will keep the original as well as creating the converted image.
As for batch conversion, I think you need to use the Mogrify tool which is part of ImageMagick.
Keep in mind that this overwrites the old images.
The command is:
mogrify -format jpg *.png
ANSWER 2
Score 126
I have a couple more solutions.
The simplest solution is like most already posted. A simple bash for loop.
for i in *.png ; do convert "$i" "${i%.*}.jpg" ; done
For some reason I tend to avoid loops in bash so here is a more unixy xargs approach, using bash for the name-mangling.
ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.*}.jpg"'
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).
ls -1 *.png | parallel convert '{}' '{.}.jpg'
The number of jobs defaults to the number of CPU cores you have. I found better CPU usage using 3 jobs on my dual-core system.
ls -1 *.png | parallel -j 3 convert '{}' '{.}.jpg'
And if you want some stats (an ETA, jobs completed, average time per job...)
ls -1 *.png | parallel --eta convert '{}' '{.}.jpg'
There is also an alternative syntax if you are using GNU Parallel.
parallel convert '{}' '{.}.jpg' ::: *.png
And a similar syntax for some other versions (including debian).
parallel convert '{}' '{.}.jpg' -- *.png
ANSWER 3
Score 30
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:
for img in *.png; do
filename=${img%.*}
convert "$filename.png" "$filename.jpg"
done
ANSWER 4
Score 9
The actual "png2jpg
" command you are looking for is in reality split into two commands called pngtopnm
and cjpeg
, and they are part of the netpbm
and libjpeg-progs
packages, respectively.
png2pnm foo.png | cjpeg > foo.jpeg