Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Batch Convert Images From PNG to JPEG

DZone's Guide to

How to Batch Convert Images From PNG to JPEG

To recursively scan a directory tree for PNG images and convert them to JPEG format, you simply need to use the find Shell command and ImageMagick's convert utility.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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. Optionally, we can add GNU Parallel to speed up processing time.

First, install ImageMagick on Debian/Ubuntu with:

$ sudo apt-get install imagemagick

Once ImageMagick is installed, CD to the root of the directory tree containing your images and run:

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

This Shell pipeline will do the following:

  • 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.

In this version, the images were converted sequentially.  If you want to speed up processing, you can use GNU Parallel to execute the image conversions across all CPU cores.

Install GNU Parallel on Debian/Ubuntu with:

$ sudo apt-get install parallel

Once installed, simply add  parallel to the Shell pipeline before the call to convert.  This is the parallel processing version:

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

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

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

And that's it! You've now batch convert your images from PNG to JPEG.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
jpg ,png ,conversion ,integration ,tutorial

Published at DZone with permission of Corey Goldberg, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}