vote up 2 vote down star

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.

flag

75% accept rate

5 Answers

vote up 8 vote down check

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|flag
+1, ImageMagick is good! – nik Nov 16 at 2:30
2  
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 at 4:11
Thanks +1, I wasn't very clear - hopefully a tiny bit better. – Wil Nov 16 at 4:29
vote up 0 vote down

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|flag
vote up 0 vote down

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|flag
1  
According to the man page for convert: "The convert program is a member of the ImageMagick(1) suite of tools." – humble coffee Nov 16 at 4:06
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 at 4:17
vote up 0 vote down

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|flag
vote up 0 vote down

my jpgs just result in a completely black image, i've tried both convert and pngtopnm and both give the same results. what gives?

link|flag

Your Answer

Get an OpenID
or
never shown

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