Does anyone know a good way to batch-convert a bunch of PNGs into JPGs in linux? (I'm using Ubuntu).
A png2jpg binary that I could just drop into a shell script would be ideal.
12 Answers
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.jpgwhich 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 11 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" ; doneFor 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' ::: *.pngAnd a similar syntax for some other versions (including debian).
parallel convert '{}' '{.}.jpg' -- *.png 11 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 4 tl;dr
For those who just want the simplest commands:
Convert and keep original files:
mogrify -format jpg *.pngConvert and remove original files:
mogrify -format jpg *.png && rm *.pngBatch Converting Explained
Kinda late to the party, but just to clear up all of the confusion for someone who may not be very comfortable with cli, here's a super dumbed-down reference and explanation.
Example Directory
bar.png
foo.png
foobar.jpgSimple Convert
Keeps all original png files as well as creates jpg files.
mogrify -format jpg *.pngResult
bar.png
bar.jpg
foo.png
foo.jpg
foobar.jpgExplanation
- mogrify is part of the ImageMagick suite of tools for image processing.
- mogrify processes images in place, meaning the original file is overwritten, with the exception of the
-formatoption. (From the site:This tool is similar to convert except that the original image file is overwritten (unless you change the file suffix with the -format option))
- mogrify processes images in place, meaning the original file is overwritten, with the exception of the
- The
- formatoption specifies that you will be changing the format, and the next argument needs to be the type (in this case, jpg). - Lastly,
*.pngis the input files (all files ending in .png).
Convert and Remove
Converts all png files to jpg, removes original.
mogrify -format jpg *.png && rm *.pngResult
bar.jpg
foo.jpg
foobar.jpgExplanation
- The first part is the exact same as above, it will create new jpg files.
- The
&&is a boolean operator. In short:- When a program terminates, it returns an exit status. A status of
0means no errors. - Since
&&performs short circuit evaluation, the right part will only be performed if there were no errors. This is useful because you may not want to delete all of the original files if there was an error converting them.
- When a program terminates, it returns an exit status. A status of
- The
rmcommand deletes files.
Fancy Stuff
Now here's some goodies for the people who are comfortable with the cli.
If you want some output while it's converting files:
for i in *.png; do mogrify -format jpg "$i" && rm "$i"; echo "$i converted to ${i%.*}.jpg"; doneConvert all png files in all subdirectories and give output for each one:
find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; echo "Converted $i to ${i%.*}.jpg"; doneConvert all png files in all subdirectories, put all of the resulting jpgs into the all directory, number them, remove original png files, and display output for each file as it takes place:
n=0; find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; fn="all/$((n++)).jpg"; mv "${i%.*}.jpg" "$fn"; echo "Moved $i to $fn"; done 6 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 find . -name "*.png" -print0 | xargs -0 mogrify -format jpg -quality 50 1 my quick solutionfor i in $(ls | grep .png); do convert $i $(echo $i.jpg | sed s/.png//g); done
Many years too late, there's a png2jpeg utility specifically for this purpose, which I authored.
Adapting the code by @Marcin:
#!/bin/sh
for img in *.png
do filename=${img%.*} png2jpeg -q 95 -o "$filename.jpg" "$filename.png"
done 0 For batch processing:
for img in *.png; do convert "$img" "$img.jpg"
doneYou will end up with file names like image1.png.jpg though.
This will work in bash, and maybe bourne. I don't know about other shells, but the only difference would likely be the loop syntax.
This is what I use to convert when the files span more than one directory. My original one was TGA to PNG
find . -name "*.tga" -type f | sed 's/\.tga$//' | xargs -I% convert %.tga %.pngThe concept is you find the files you need, strip off the extension then add it back in with xargs. So for PNG to JPG, you'd change the extensions and do one extra thing to deal with alpha channels namely setting the background (in this example white, but you can change it) then flatten the image
find . -name "*.png" -type f | sed 's/\.png$//' | xargs -I% convert %.png -background white -flatten %.jpg If your PNG is transparent, try adding a black bg before converting:
mogrify -format jpg -background black -flatten *.pngor a white bg:
mogrify -format jpg -background white -flatten *.png 1 Here's the same bash solution but with ffmpeg converting:
for i in *.png ; do ffmpeg -i "$i" "${i%.*}.jpg" ; done 4