Home About Me

Batch Converting PNG and JPG Files on Linux with ImageMagick

For image format conversion on Linux, ImageMagick is a practical and powerful tool.

On Debian-based distributions, install it with:

apt-get install imagemagick

On Fedora-based systems, use:

yum install imagemagick

If you are using another platform, you can download it from the official page: https://www.imagemagick.org/script/download.php

Once installation is complete, converting a single file is straightforward. For example, this command turns a JPG image into PNG:

convert tst.jpg tsg.png

The reverse works the same way as well.

For batch conversion, suppose a directory contains several JPG files:

root@bwgzl:/data/wwwroot/getos.org/images# ll
total 188
drwxr-xr-x  2 www www  4096 Aug  4 15:48 ./
drwxr-xr-x 27 www www  4096 Aug  4 15:32 ../
-rw-r--r--  1 www www 18778 Aug  4 15:43 clip_image002.jpg
-rw-r--r--  1 www www 17637 Aug  4 15:43 clip_image004.jpg
-rw-r--r--  1 www www 28075 Aug  4 15:43 clip_image006.jpg
-rw-r--r--  1 www www 30916 Aug  4 15:43 clip_image008.jpg
-rw-r--r--  1 www www 13538 Aug  4 15:43 clip_image010.jpg
-rw-r--r--  1 www www 11845 Aug  4 15:43 clip_image012.jpg
-rw-r--r--  1 www www 14519 Aug  4 15:43 clip_image014.jpg
-rw-r--r--  1 www www 33052 Aug  4 15:43 clip_image016.jpg
root@bwgzl:/data/wwwroot/getos.org/images# ls -1 *.jpg | xargs -n 1 bash -c 'convert "$0" "${0%.jpg}.png"'
root@bwgzl:/data/wwwroot/getos.org/images# ls -ltr
total 1172
-rw-r--r-- 1 www  www   18778 Aug  4 15:43 clip_image002.jpg
-rw-r--r-- 1 www  www   17637 Aug  4 15:43 clip_image004.jpg
-rw-r--r-- 1 www  www   28075 Aug  4 15:43 clip_image006.jpg
-rw-r--r-- 1 www  www   30916 Aug  4 15:43 clip_image008.jpg
-rw-r--r-- 1 www  www   13538 Aug  4 15:43 clip_image010.jpg
-rw-r--r-- 1 www  www   11845 Aug  4 15:43 clip_image012.jpg
-rw-r--r-- 1 www  www   14519 Aug  4 15:43 clip_image014.jpg
-rw-r--r-- 1 www  www   33052 Aug  4 15:43 clip_image016.jpg
-rw-r--r-- 1 root root 132411 Aug  4 16:00 clip_image002.png
-rw-r--r-- 1 root root 106702 Aug  4 16:00 clip_image004.png
-rw-r--r-- 1 root root 197143 Aug  4 16:00 clip_image006.png
-rw-r--r-- 1 root root  79397 Aug  4 16:00 clip_image008.png
-rw-r--r-- 1 root root  37643 Aug  4 16:00 clip_image010.png
-rw-r--r-- 1 root root  81090 Aug  4 16:00 clip_image012.png
-rw-r--r-- 1 root root  88635 Aug  4 16:00 clip_image014.png
-rw-r--r-- 1 root root 272537 Aug  4 16:00 clip_image016.png

The second command in that sequence is the actual batch conversion step:

ls -1 *.jpg | xargs -n 1 bash -c 'convert "$0" "${0%.jpg}.png"'

It lists all .jpg files, then passes them one by one to convert, keeping the original filename and replacing only the extension with .png.

After it finishes, the directory will contain the original JPG files alongside the newly generated PNG versions.