Batch Converting jp2 images to jpgs, and pdfs using ImageMagick

Le Tue 15 January 2019

Download the pdf button

From time to time you may have a need to convert a series of jpeg2000 or .jp2 images to a more accessable format such as .jpgs or .pngs. The ultra-powerful commandline image processing tool, ImageMagick, is ideal for this. ImageMagick is available for Linux, Mac, iOS, and Windows. The examples shown here are on a Linux operating system, but the same commands should work for the other operating systems.

Assuming you have your selection of .jp2 files in a directory it is worth creating a new directory within it to hold your processed image files. In this example I have created the Processed directory to receive the .jpgs.

Directory Structure for the example

To process the .jp2 files we will use ImageMagick version 6’s batch processing mogrify command. In ImageMagick version 7, the mogrify command is still present as a legacy function but as I understand it the new command is magick mogrify. Mogrify is a slightly dangerous command because it can overwrite the original files. Because we are converting the images into a different format with different file extension and putting them into a new directory, this is not a problem. The mogrify command as we will use it here will process all files in the active directory with the file extension .jp2. This means the .jp2 files can have any filename and don’t need to conform to any common naming convention. Mogrify preserves the base filenames through the process.

Opening a command line console in the directory with our .jp2 files – in my example I have the .jp2 files in the /home/hamy/Documents/JP2_Wrangling/2DArtist-66 directory. We use the command;

mogrify -path '/home/hamy/Documents/JP2_Wrangling/2DArtist_066/Processed' -verbose -quality 95 -format jpg *.jp2  

-path

This is the directory were the processed files will be deposited. If it is left out, the processed files will be put into the directory alongside the .jp2 source files.


-verbose

This simply provides a bit more feedback about how far through the process you are. It also provides potentially useful information about the image sizes being processed.


-quality

is for specifying the quality of the .jpgs being produced. The lower the number the worse the quality but the smaller the file size.


-format

is where the output format is specified.


Console Output During Process

As you can see from the console output, the -verbose option is reporting the image it has processed, and useful things like the image size, and file size. Converting from .jp2 to .jpg will result in significantly larger image files being produced.


A couple of other useful mogrify options are;

mogrify -help

which yields a large list of settings, operators, and options for using with mogrify.

-resize, -scale, -crop are all useful for further processing the images. For instance you may find the .jp2 file is quite high resolution. Including -resize with some pixel sizes or a percentage will change the resolution of your image. In the example below the .jpg files produced would be resized to best fit one or other of the 2480 x 3580 pixel dimensions while maintaining the image aspect ratio.

mogrify -path '/home/hamy/Documents/JP2_Wrangling/2DArtist_066/Processed' -verbose -resize 2480x3580 -quality 95 -format jpg *.jp2  

At the end of the process you will have a directory full of .jpg files.

Directory full of processed jpg files

Once you have your directory of images processed to .jpgs, you can then use ImageMagick to compile them into a pdf, or you can simply zip them up and create a Comic Book Zip (.cbr) or Comic Book Rar (.cbr) format ebook.

The most basic command structure for turning the images into a .pdf is;

convert *.jpg Output.pdf

Adding the -verbose option here is useful for keeping track of progress. I believe there are occasionally errors thrown up by this simplistic convert command structure for building pdfs, but I have yet to observe them. As I understand it the equivalent ImageMagick version 7 command would be;

magick *.jpg Output.pdf

Comic Book Formats – .cbr and .cbz

I feel these are my new best friend for packaging bunches of images as an ebook. I am not sure what opens them in Windows and Macs but presume the Adobe Reader probably does. In the Linux world, I find Okular a great viewer for ebook formats including .cbr and .cbz formats. The advantage of the comic book formats is that they are quicker to handle than image-heavy pdfs.

To turn your series of images into a comic book format, just zip (or rar) them up, and change the filename extension to either .cdz or .cbr depending on whether you zipped it or rar-ed it.

Par Hamish Trolove, Catégorie : Tutorials

Tags : ImageMagick / Graphics / Tools /