0

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)
Siva
  • 1,078
  • 4
  • 18
  • 36
  • That link is only for a the binary install of ImageMagick on El Capitan. If you are on a different Mac OSX system, then use either MacPorts or Homebrew to install your binary (with all the delegates included). If it works in command line, then it should work in other APIs. What was the error message that you got? Sorry, I only know the command line. – fmw42 Jun 19 '17 at 01:28
  • I have installed using home-brew. brew install ImageMagick. But I provided that path as well in run configurations and set system property. It is still showing the same error. – Siva Jun 19 '17 at 01:32
  • Error: org.im4java.core.InfoException: org.im4java.core.CommandException: java.io.IOException: Cannot run program "identify": error=2, No such file or directory – Siva Jun 19 '17 at 01:34
  • In ImageMagick 7, you need to preface identify with magick. So that it is magick identify. You have to put magick in front of all commands, except convert. For convert, replace convert with simply magick. See http://imagemagick.org/script/porting.php#cli – fmw42 Jun 19 '17 at 02:34
  • I added magick infront of command: Still it is showing the same error. Command: magick compare imageone.png imagetwo.png diffimage.png – Siva Jun 19 '17 at 09:40
  • Try magick compare -metric rmse imageone.png imagetwo.png diffimage.png. But note that both input images must be the same size. If that does not work, then I think you have not installed ImageMagick properly or completely. Can you do: magick logo: logo.gif and do you get error messages. If not, is the image created? – fmw42 Jun 19 '17 at 19:14

0 Answers0