February 2, 2017

Batch Convert Images from PNG to JPEG

This post briefly shows how to recursively scan a directory tree for PNG images and convert them to JPEG format.  To achieve this, we use the `find` shell command and ImageMagick's `convert` utility.  We use GNU Parallel to speed up processing time by executing conversions in parallel.  This should make efficient use of all CPU cores.

First, install ImageMagick and GNU Parallel.  For example, on Debian/Ubuntu you would run:

$ sudo apt-get install imagemagick
$ sudo apt-get install parallel

Once these are  installed, `cd` to the root of the directory tree containing your images, and run:

$ find . -iname "*.png" | parallel convert -quality 95% {} {.}.jpg

This shell pipeline will:

* recursively search under the current directory
* match files that have the [case-insensitive] `.png` extension
* convert each PNG image to JPEG format at 95% quality
* save the new image with the file extension changed to `.jpg`

Once converted, you can delete the original PNG images with:

$ find . -iname "*.png" | parallel rm {}

No comments: