batch convert png to jpg in linux

How to Batch Convert PNG to JPG in Linux

PNG and JPG files are both popular image formats used by organizations and individuals. Sometimes you may need to convert PNG to JPG and vice versa for compatibility with your software. There are many online sites that allow you to convert single PNG to JPG and vice versa. But sometimes you may need to batch convert PNG to JPG and JPG to PNG. For this purpose, you need to use an image editing tool so that you can add its command to your script or application. There are many tools to do this in Linux. In this article, we will learn how to batch convert PNG to JPG using ImageMagick tool.


How to Batch Convert PNG to JPG in Linux

Here are the steps to batch convert PNG to JPG in Linux.


1. Install ImageMagick

$ sudo apt update
$ sudo apt install imagemagick

Alternatively, you can compile ImageMagick from source. Download ImageMagick tar.gz file from here.

Extract the tarball you have downloaded.

$ tar -xvf ImageMagick.tar.gz
$ cd ImageMagick-7.0.10-11/

Run the following command to compile ImageMagick.

$ ./configure
$ make

Run the following command to install ImageMagick.

$ sudo make install

Run the following command to configure dynamic run-time bindings.

$ sudo ldconfig /usr/local/lib


2. Batch Convert PNG to JPG

ImageMagick provides many useful tools for image editing. convert and mogrify are two such tools for this purpose.

Here is the syntax to use convert command to convert PNG to JPG.

$ convert [options] /path/to/png /path/to/jpg

In the above command, you need to specify the source image’s path after options, followed by the destination path of converted image.

Here is a sample command to convert /home/ubuntu/test.png to /home/ubuntu/test.jpg.

$ convert /home/ubuntu/test.png /home/ubuntu/test.jpg

Similarly, if you want to convert a JPG image to PNG, you can use the following command.

$ convert [options] /path/to/jpg /path/to/png

Here is an example.

$ convert /home/ubuntu/test.jpg /home/ubuntu/test.png

But please note, convert command can convert only one file at a time. So if you want to batch convert files using convert command, you need to run it within a loop that goes through your image files.

ImageMagick also provides another tool, mogrify that is perfect for batch conversion. Here is the syntax for mogrify.

$ mogrify [options] /path/to/png 

In the above command, you need to specify only the path to png image. Here is an example to convert /home/ubuntu/test.png to /home/ubuntu/test.jpg using mogrify.

$ mogrify -format jpg /home/ubuntu/test.png

In the above command, we use -format command to specify the format of converted image file.

Please note, mogrify will not replace your source image with converted image file. That is, after you run the above command, there will be both /home/ubuntu/test.jpg and /home/ubuntu/test.png.

If you want to save the converted file as a separate file and not replace the original file, you need to specify the destination path to converted file using -path option.

$ mogrify -format jpg -path /home/data /home/ubuntu/test.png

When you run the above command, mogrify will create test.jpg in /home/data and leave /home/ubuntu/test.png as it is.

Now, if you want to batch convert all .png images in /home/ubuntu you can use wildcard character while specifying source path in mogrify command.

$ mogrify -format jpg /home/ubuntu/*.png

The above command will convert and replace all PNG files to JPG files in /home/ubuntu. If you don’t want original files to be replaced but want to create a separate copy of converted files, you can use the -path option to specify the path to be used to store converted files.

$ mogrify -format jpg /home/data /home/ubuntu/*.png

In this article, we have learnt how to batch convert PNG to JPG images using ImageMagick Library. Both convert & mogrify support many options such as size, color scale, rotation, etc. To read more about the different options available for convert and mogrify, refer to their man pages.

Also read:

How to Convert Webp to Gif in Linux
How to Convert Images to Webp in Linux
How to Convert Images to Webp in Python
How to Check if User Has Sudo Access
How to Combine JSON Files to CSV

2 thoughts on “How to Batch Convert PNG to JPG in Linux

  1. “Please note, mogrify will replace your source image with converted image file. That is, after you run the above command, there will be only /home/ubuntu/test.jpg and no /home/ubuntu/test.png.”

    This is wrong – `mogrify -format jpg/png ./test-picture.jpg/png` will add the desired conversion.
    Conclusion: After the command you will have .png as well as .jpg in your directory.

Leave a Reply

Your email address will not be published. Required fields are marked *