Although PNG offers a better image quality compared with JPG, the large size factor is usually a concern for Internet users. This is typically why PNG images are converted to JPG.
In this article, you will learn how to convert multiple PNG images to JPG format from the Linux command line. We will use 2 tools, namely; ImageMagick and GraphicsMagick.
Install Imagemagick in Linux
ImageMagick is a free tool for creating, modifying, and converting a wide variety of image file formats including PNG. To install ImageMagick on your Linux machine, run the following command as per your Linux distribution.
$ sudo apt install imagemagick [On Debian/Ubuntu & Mint] $ sudo dnf install ImageMagick [On RHEL/CentOS & Fedora]
Batch Convert PNG to JPG with ImageMagick
First, change into the directory containing the PNG files with the cd command. After that, you may use the command below to list the content of the directory.
$ ls -lh
Next, run the command below to convert all PNG files in the current directory to JPG.
$ mogrify -format jpg *.png
Now, run the command below again to see the results.
$ ls -lh
Looking at the file size column in the image above, the difference is clear. As a side note, you can convert a single file as follows.
$ convert file.png file.jpg
Feel free to read the manual pages and discover what more you could do with the ImageMagick tool.
$ man imagemagick
Install GraphicsMagick in Linux
GraphicsMagick was derived from ImageMagick but it offers faster processing and better quality. To install GraphicsMagick on your Linux machine, run the following command as per your Linux distribution.
$ sudo apt install graphicsmagick [On Debian/Ubuntu & Mint] $ sudo yum install epel-release [On RHEL/CentOS] $ sudo dnf install GraphicsMagick [On RHEL/CentOS & Fedora]
Batch Convert PNG to JPG with GraphicsMagick
Begin by changing into the directory containing the PNG files and then run the command below to list the content of the directory.
$ ls -lh
Now, to convert all PNG files in the current directory to JPG format, run the following command.
$ sudo gm mogrify -format jpeg *.png
And check the results with the following command.
$ ls -lh
If you would like to convert a specific file from PNG to JPEG, you can run the command below.
$ gm convert file.png file.jpeg
To find out what more you can do with the GraphicsMagick tool, run the command below to view the manual pages.
$ man gm
Conclusion
We have shown you how to convert multiple PNG files into JPG format in this guide. While doing this can save you some network bandwidth and storage space, the image quality will be affected.