Convert all non-JPG images to JPGs

commandconversionimages

What command can I use to convert all images in a folder which are not JPGs (PNG and BMP primarily) to JPG? I'd also like the conversion quality to be 100%. And I'd like the converted images to replace the originals.

Best Answer

Assuming there are only images in that folder, you can

ls | grep -v jpg$

to get all filenames that do not end with jpg, which I assume are all the images you want to convert. Then you can use the tool convert from ImageMagick like this

ls | grep -v jpg$ | while IFS= read -r FILENAME
do
    convert "${FILENAME}" "${FILENAME%.*}.jpg"
done

The convert command expands to convert <file name as printed by ls> <file name without extention>.jpg. The extention jpg will tell convert to convert to jpg format.

Related Question