Showing posts with label image processing. Show all posts
Showing posts with label image processing. Show all posts

Sunday, January 13, 2019

Using Graphics Magick with Java to Convert, Resize, Blur, Rotate and Perform Basic Image Processing operations




Hello, Friends today in this blog we will be learning how to use Graphics Magick to perform the basic operation on Images using Java.

What You will need


How the Graphics Magick & IM4Java works with each other

So before we get our hands dirty in coding lets first understand, How things work. Graphics Magick is Command line Utility which is used for image processing. We can perform many image processing operation like resize, shrink images, drawing shapes, annotate images, blurring images, rotating images & so on. It has rich sets of function by which one can develop a full-fledged image editing software like Photoshop. IM4Java is Java Wrapper build around Graphics Magick to take input from the user and convert the input to appropriate Graphics Magick commands.



Install Graphics Magick on your system

  • Download Graphics Magick from here & install on your system.
  • Make Sure that you have added the path of Graphics Magick installation directory to PATH environment variable as per your Operating System.
  • Open Terminal / Command prompt & Verify graphics magick is accessible by command "gm -version"  



Let's get started with coding, Here is our build.gradle file

apply plugin: 'java'
apply plugin: 'application'

dependencies {
    compile group: 'org.im4java', name: 'im4java', version: '1.4.0'
    testImplementation 'junit:junit:4.12'
}

mainClassName='org.javahotfix.gm.app.SimpleGraphicsOperation'

sourceCompatibility=1.8
targetCompatibility=1.8

repositories {
    jcenter()
}

  • Here we will be writing our code in SimpleGraphicsOperation class
  • We have added the dependency on im4java in build.gradle on line number 5.

Here is First Small Example to Resize Image to create its Thumbnail (SimpleGraphicsOperation.java)

public void resizeImage(String originalFile,
                 String targetFile,
                 int width,
                 int height) throws InterruptedException, IOException, IM4JavaException {
  ConvertCmd command = new ConvertCmd(true);
  IMOperation operation = new IMOperation();

  operation.addImage(originalFile);
  operation.resize(width, height);
  operation.addImage(targetFile);

  // Execute the Operation
  command.run(operation);
}


In the above example, we have created an object of ConvertCmd having a boolean constructor with value true. This will tells IM4Java library to use Graphics Magick. By Default IM4Java uses Image Magick for performing its operation.

IMOperation class used to generate equivalent graphics magick command for a various operation like resize, blur, draw border etc. check out more operation here.


Working with Streams (SimpleGraphicsOperation.java)
If you are working with stream-based application & don't have a physical file path, then you can use below example where we have used the stream-based approach.
public void resizeImage(
      InputStream input, 
      OutputStream output, 
      int width, 
      int height,
      String format) throws IOException, InterruptedException, IM4JavaException {
     
     ConvertCmd command = new ConvertCmd(true);
     
     Pipe pipeIn = new Pipe(input, null); 
     Pipe pipeOut = new Pipe(null, output);
     
     command.setInputProvider(pipeIn);
     command.setOutputConsumer(pipeOut);
     
     IMOperation operation = new IMOperation();
     operation.addImage("-");
     operation.resize(width, height);
     operation.addImage(format + ":-");
     
     command.run(operation);
 }

Adding Blur Effect to Image having border (SimpleGraphicsOperation.java)
public void performMultipleOperation(
      InputStream input, 
      OutputStream output) throws IOException, InterruptedException, IM4JavaException {
     ConvertCmd command = new ConvertCmd(true);
     Pipe pipeIn = new Pipe(input, null); 
     Pipe pipeOut = new Pipe(null, output);
     
     command.setInputProvider(pipeIn);
     command.setOutputConsumer(pipeOut);
     
     IMOperation operation = new IMOperation();
     operation.addImage("-");
     operation.blur(100.0);  // Radius of Blur effect
     operation.border(15,15); // width height parameter
     operation.addImage("-");
     
     command.run(operation);
}


Join multiple images horizontally (SimpleGraphicsOperation.java)
public void joinMultipleImages(
      InputStream input1,
      InputStream input2,
      InputStream input3,
      InputStream input4,
      String output) throws IOException, InterruptedException, IM4JavaException {
 
     BufferedImage img1 = ImageIO.read(input1);
     BufferedImage img2 = ImageIO.read(input2);
     BufferedImage img3 = ImageIO.read(input3);
     BufferedImage img4 = ImageIO.read(input4);
       
     ConvertCmd command = new ConvertCmd(true); 
     IMOperation operation = new IMOperation();
     operation.addImage();  // placeholder for first image
     operation.addImage();  // placeholder for second image
     operation.addImage();  // placeholder for third image
     operation.addImage();  // placeholder for fourth image
     operation.appendHorizontally();  // perform operation
     operation.addImage();  // placeholder for output image
     command.run(operation, img1, img2, img3, img4, output); // provide parameter base on placeholder parameter index
}

Thats all for now guys. I hope you have got a basic understanding of how we can use Graphics Magick with java through IM4Java. Hope you will do some more experiments on this. Below are the List of References that have been mention in this blog.