ImageMagick: Im4Java Running from eclipse from MacBook is showing the exception

1.8k views Asked by At

I'm trying to run im4java to compare the images but I'm getting the below exception message.

I have followed the steps to add ImageMagick as in this link: https://www.imagemagick.org/script/download.php#macosx

I was getting the same error so, I added the environmental variable in the run configurations of my eclipse as:

DYLD_LIBRARY_PATH=/Users/siva/Downloads/ImageMagick-7.0.5/lib and started running but still i'm getting the same error message:

I also tried:

 System.setProperty("java.library.path", "/Users/siva/Downloads/ImageMagick-7.0.5/bin/magick");

If anyone has tried using imageMagick on Mac please suggest me what are the steps I have to do.

I was able to run the below command from the terminal successfully: compare imageone.png imagetwo.png diffimage.png

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.ImageMagickCmd;
import org.im4java.core.Info;
import org.im4java.process.ArrayListOutputConsumer;

public class UseImageM {
 private static final String COMPARE_COMMAND = "compare";
 private static final String ABSOLUTE_ERROR = "AE";

 private static String imageOnePath = "/Users/siva/Documents/imageEclipse/UsingImagemagick/screenshot.png";
 private static String imageTwoPath = "/Users/siva/Documents/imageEclipse/UsingImagemagick/screenshotfinal.png";
 private static String outputImageName = "outputoftheimageCompare";
 private static final int COUNT_OF_DECIMAL = 10;

 public static void main(String[] args) {
  IMOperation imOperation = new IMOperation();
  imOperation.metric(ABSOLUTE_ERROR);
  imOperation.addImage(imageOnePath);
  imOperation.addImage(imageTwoPath);
  imOperation.addImage(outputImageName);

  ImageMagickCmd compare = new ImageMagickCmd(COMPARE_COMMAND);
  ArrayListOutputConsumer outputConsumer = new ArrayListOutputConsumer();
  compare.setOutputConsumer(outputConsumer);
  BigDecimal diffPercentage = BigDecimal.ZERO;

  Info imageInfo;
  long imageSize = 0;
  try {
   imageInfo = new Info(imageTwoPath, true);

   imageSize = imageInfo.getImageWidth() * imageInfo.getImageHeight();

   compare.run(imOperation);

   final ArrayList<String> errorText = compare.getErrorText();
   if (errorText.size() == 1) {
    final BigDecimal diffCount = new BigDecimal(errorText.get(0));
    if (!BigDecimal.ZERO.equals(diffCount)) {
     diffPercentage = diffCount.divide(
       new BigDecimal(imageSize), COUNT_OF_DECIMAL,
       BigDecimal.ROUND_DOWN);
    }
   }
  } catch (final IOException | InterruptedException e) {
   System.out.println((String.format(
     "Exception happened in comparison between %s and %s.",
     imageOnePath, imageTwoPath) + e));

  } catch (final IM4JavaException e) {
   e.printStackTrace();

   // error message
   if (!StringUtils.isNumeric(compare.getErrorText().get(0))) {
    final String errorMsg = String.format(
      "Exception happened in comparison between %s and %s.",
      imageOnePath, imageTwoPath);
    System.out.println(errorMsg + e);

   }

   final BigDecimal diffCount = new BigDecimal(compare.getErrorText()
     .get(0));
   diffPercentage = diffCount.divide(new BigDecimal(imageSize),
     COUNT_OF_DECIMAL, BigDecimal.ROUND_DOWN);
  }

  System.out.println(diffPercentage);

 }

}


Console Logs:

org.im4java.core.InfoException: org.im4java.core.CommandException: java.io.IOException: Cannot run program "identify": error=2, No such file or directory
 at org.im4java.core.Info.getBaseInfo(Info.java:360)
 at org.im4java.core.Info.<init>(Info.java:151)
 at UseImageM.main(UseImageM.java:36)
Caused by: org.im4java.core.CommandException: java.io.IOException: Cannot run program "identify": error=2, No such file or directory
 at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
 at org.im4java.core.Info.getBaseInfo(Info.java:342)
 ... 2 more
Caused by: java.io.IOException: Cannot run program "identify": error=2, No such file or directory
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
 at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:407)
 at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312)
 at org.im4java.core.ImageCommand.run(ImageCommand.java:215)
 ... 3 more
Caused by: java.io.IOException: error=2, No such file or directory
 at java.lang.UNIXProcess.forkAndExec(Native Method)
 at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
 at java.lang.ProcessImpl.start(ProcessImpl.java:134)
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
 ... 6 more
Exception in thread "main" java.lang.NullPointerException
 at UseImageM.main(UseImageM.java:60)

0

There are 0 answers