Sharing Our Passion for Technology
& Continuous Learning
Image Processing Using ImageMagick and JMagick
Introduction to ImageMagick
ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.
The functionality of ImageMagick is typically utilized from the command line. In this blog I am focussing on how to use Java with ImageMagick. There are two options available to use ImageMagick
- JMagick provides an object-oriented Java interface to ImageMagick which I am going to show in this blog.
- Calling the ImagicMagick directly as the Command line using
Runtime.getRuntime().exec(command);
Jmagick
JMagick is an open source Java interface of ImageMagick. It is implemented in the form of Java Native Interface (JNI) into the ImageMagick API.JMagick does not attempt to make the ImageMagick API object-oriented. It is merely a thin interface layer into the ImageMagick API.
Image Conversion using Jmagick
This function shows how to convert one file format to other format mainly I am focusing on PDF to TIFF conversion. Conversion of PDF into multiple page TIFF or single page TIFF and also the function is also extensible for accepting Compression Format such as GROUP4, FAX or JPEG.
public void convert(File inputFile, File outputDirectory, ImageType outputType, boolean multiple) {
if (inputFile != null && inputFile.exists() && ImageUtil.isValidMime(inputFile)) {
try {
ImageInfo info = getImageInfo(inputFile);
String fileName = inputFile.getName();
fileName = inputFile.getName().split("\\.")[0];
if (multiple) {
MagickImage image = new MagickImage(info);
MagickImage[] imArray = image.breakFrames();
for (int i = 0; i < imArray.length; i++) {
StringBuilder outputFile = new StringBuilder(outputDirectory.getAbsolutePath());
outputFile.append(File.separatorChar);
File file = new File(outputFile.toString());
imArray[i].setFileName(file.getAbsolutePath());
imArray[i].writeImage(info);
}
} else {
StringBuilder outputFile = new StringBuilder(outputDirectory.getAbsolutePath());
outputFile.append(File.separatorChar);
outputFile.append(fileName);
outputFile.append(".");
outputFile.append(outputType.name().toLowerCase());
File file = new File(outputFile.toString());
report.addOutputFile(file);
info.setAdjoin(1);
MagickImage image = new MagickImage(info);
image.setFileName(file.getAbsolutePath());
image.writeImage(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private ImageInfo getImageInfo(File inputName) throws MagickException {
String density = this.getProperties().getProperty(IMAGEMAGIC_DENSITY, "300");
ImageInfo info = new ImageInfo(inputName.getAbsolutePath());
info.setDensity(density);
info.setCompression(CompressionType.Group4Compression);
info.setColorspace(ColorspaceType.RGBColorspace);
return info;
}