How to batch convert PNG to JPG on Linux using ImageMagick


Converting multiple PNG image files to JPG one by one can be tiring and time-consuming. Fortunately, you can easily do it at once via command line using ImageMagick. ImageMagick is free and open-source software to manipulate image files available on most Linux distros. You can use it to convert, flip, rotate, and resize image files with ease.

Installing ImageMagick

First of all, install ImageMagick on your computer in order to use the mogrify command. The following is the command line for Debian-based distros. If you’re using a different distro, make sure to have the ImageMagick package installed.

sudo apt-get update
sudo apt-get install imagemagick

Creating the output directory

Create a new directory for the output JPG image files in order to avoid them from mixing up with the PNG files in the working directory.

mkdir output

Converting PNG to JPG using ImageMagick

Use the following command to convert PNG image files to JPG. Setting the quality to 80 helps optimize the output image file size, so it’s recommended.

And be careful about the input PNG file extension in the working directory because it’s case-sensitive. Make sure they all have the same extension in either lowercase or UPPERCASE. If some of them are ending with the uppercase .PNG, the command below will skip them and read only the lowercase .png image files. So you may have to convert them twice with the different extensions.

mogrify -path output -format jpg -quality 80 *.png

If you would like to resize the output JPG image files during conversion; for example, to 50% of the original size, you can add -resize 50% to the command line. You can change the output size to your preferred number.

mogrify -path output -format jpg -resize 50% -quality 80 *.png