Using ImageMagick for batch format conversion

As I ramp up my blogging output, I need to deal with more and more images, mostly screenshots. On Ubuntu, the screenshot tool saves in PNG format. This is a great image format for lossless compression, but most of the time I do not care all that much about pristine image quality. I just want small images suitable for web content documenting procedures etc. So, I need a utility that can quickly and easily convert the PNGs to a format with a higher compression ratio.

ImageMagick is attractive for this purpose, but I have had a hell of a time figuring out how to batch convert my PNGs to JPEG. Here’s the solution I came up with.

In directory ~/images I have a bunch of PNG images. I want to convert all the PNG images to JPEG format, and I do not want to overwrite the original PNGs. Instead, I want to output the JPEG images to a destination directory ~/images/jpeg-conversion. Furthermore, I want all the output JPEG filenames to be the same as their corresponding PNG filenames — just with a .jpg extension instead of .png.

The following command accomplishes this.

$ mogrify -format jpg -path ~/images/jpeg-conversion/ *.png

You might initially be fooled, as I was, into thinking that the convert program would logically be the correct one to do a batch image format conversion. However, I was not able to find a way to very simply convert and carry forward the input filenames to the corresponding output filenames. With mogrify, this was simple.

The ImageMagick documentation appears to be pretty comprehensive, yet it was very difficult to parse out a solution to this batch conversion problem. I eventually landed on the solution on a page discussing thumbnail creation:

For example, this converts JPG images into GIF thumbnails in a "thumbs" sub-directory that was just created.

mkdir thumbs

mogrify -format gif -path thumbs -thumbnail 100x100 *.jpg

Interestingly, the author goes on to state the following:

The other methods will require you to either, first make a copy of the original image, before running “mogrify“, create a special script to process the images, or some other DIY method. A number of the simpler non-IM techniques are detailed at the end of the example section for Batch Processing – Without using “mogrify.

Magick, indeed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s