up vote 8 down vote favorite
3
share [g+] share [fb]

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.

link|improve this question

75% accept rate
feedback

4 Answers

up vote 15 down vote accepted

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

and it will keep the original as well as creating the converted image. As for batch. I think you need to use the Morgify tool (from the same command line when in imagemagick.

The command is:

mogrify -format png *.jpg
link|improve this answer
+1, ImageMagick is good! – nik Nov 16 '09 at 2:30
3  
Awesome, that's exactly what I was after and will be using again. By the way, just to clarify as I didn't realise this is what you meant: convert is used to generate a separate output file, mogrify is used to modify the original image. – humble coffee Nov 16 '09 at 4:11
Thanks +1, I wasn't very clear - hopefully a tiny bit better. – William Hilsum Nov 16 '09 at 4:29
Is Morgify a typo? – Hello71 Jul 19 '10 at 16:37
png images with transparent background does not convert properly to jpg. – vishnu Nov 28 '11 at 4:33
show 3 more comments
feedback

There is no reason to install ImageMagick if it won't ever be used again. The linux convert command, as pointed out by Jeffrey, is enough. To avoid that double extension problem this is what the bash code should look like:

for img in *.png; do
    filename=${img%.*}
    convert "$filename.png" "$filename.jpg"
done
link|improve this answer
2  
According to the man page for convert: "The convert program is a member of the ImageMagick(1) suite of tools." – humble coffee Nov 16 '09 at 4:06
1  
You are correct. For some reason I thought it was part of a different library. Either way the code I posted above is the correct way to automate batch conversion within a directory. – Marcin Nov 16 '09 at 4:17
feedback

For batch processing:

for img in *.png; do
  convert "$img" "$img.jpg"
done

You 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.

link|improve this answer
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.