0

How do you run CompareCmd in Java from imageMagick version 7? The previous versions running compare would run compare, but now in version 7 running compare runs magick compare, which throws error magick: no images found for operation `-metric' at CLI arg 1 @ error/operation.c/CLIOption/5227. Please advise. If I add in op.addRawArgs("compare"); then run the comparecmd I get org.im4java.core.CommandException: java.lang.NullPointerException at org.im4java.core.ImageCommand.run(ImageCommand.java:219)

CompareCmd compare = new CompareCmd();
compare.setSearchPath(imageMagickBinPath);
compare.setErrorConsumer(StandardStream.STDERR);

IMOperation op = new IMOperation();
op.addRawArgs("compare");
op.metric("AE");
op.fuzz(10.0);

op.addImage(screenShotPath1);
op.addImage(screenShotPath2);
op.addImage(imageCompareResultFile.getAbsolutePath());

    //Execute the Operation
    try {
        compare.run(op);
        isSame=true;
    } 
    catch (Exception e) 
    {
        isSame=false;
    }
1
  • Can you paste the full stack trace? Also which version of im4java do you use?
    – Alexey R.
    Jul 23, 2018 at 10:01

1 Answer 1

0

I got it to work with imageMagick 7 and I got a 3rd file that showed the red differences or a gray image for no differences aka the same pics. I split the exception I got after running compare command to retrieve the number >0 to verify for differences and inf, blank, or null for no differences returned aka when there are same pics.
CompareCmd compare = new CompareCmd(); compare.setSearchPath(imageMagickBinPath); //compare.setErrorConsumer(StandardStream.STDERR); //don't use or can't get the values to compare if different or same

IMOperation op = new IMOperation();
op.addRawArgs("compare"); //additional for imageMagick 7.0
op.metric("AE");
op.fuzz(1.0);
op.addImage(screenShotPath1);
op.addImage(screenShotPath2);
op.addImage(imageCompareResultFile.getAbsolutePath());

//Running compare.run(op) returns a difference file
//If it returns a difference file then the pictures are not the same and isSame is set to false
//If it returns an error then the pictures are the same and isSame is set to true

try {
    compare.run(op);
} catch (IOException e1a) {
    e1a.printStackTrace();
    Reporter.log("--Got error "+e1a.getMessage().toString(), true);
        } catch (InterruptedException e1b) {
            e1b.printStackTrace();
            Reporter.log("--Got error "+e1b.getMessage().toString(),true);
        } catch (IM4JavaException e1c) {
            e1c.printStackTrace();
            Reporter.log("--Got error "+e1c.getMessage().toString(),true);
        }

        IMOperation op2 = new IMOperation();
        Reporter.log("--Compare the differences file produced by compare command to see if there are differences or if the pics are the same.", true);
        op2.addRawArgs("compare"); //additional for imageMagick 7.0
        op2.channel("red");
        op2.metric("PSNR");
        op2.addImage(screenShotPath1);
        op2.addImage(screenShotPath2);
        op2.addImage(imageCompareResultFile.getAbsolutePath());
        try {
            compare.run(op2);
        } catch (IOException e2a) {
            // TODO Auto-generated catch block
            e2a.printStackTrace();
            Reporter.log("--Got error "+e2a.getMessage().toString(), true);
        } catch (InterruptedException e2b) {
            // TODO Auto-generated catch block
            e2b.printStackTrace();
            Reporter.log("--Got error "+e2b.getMessage().toString(), true);
        } catch (IM4JavaException e2c) {
            // TODO Auto-generated catch block
            e2c.printStackTrace();
            Reporter.log("--Got error "+e2c.getMessage().toString(), true);
            howDifferent = e2c.getMessage().toString();
        }

        Reporter.log("--If imagemagick returns an exception with a value returned, split the exception and the value apart by the : into an array to use for checking later.", true);
        Reporter.log("--imagemagick returns nothing, null, or inf if pics are same", true);
        Reporter.log("--imagemagick returns a decimal number if pics are different", true);

if (howDifferent == "")
        {
            //returns blank if pics are the same
            isSame=true;
            Reporter.log("--image compare the amount of differences is not greater than 0: blank is returned.", true);
        }
        else if (howDifferent == null)
        {
            //returns null if pics are the same
            isSame=true;
            Reporter.log("--image compare the amount of differences is not greater than 0: null is returned.", true);
        }
        else 
        {
             String [] errarray = howDifferent.split(":");
             try
             {
                    //Getting error org.im4java.core.CommandException: 3.50752
                    //returns >0 if pics are different
                    differentValue = Double.parseDouble(errarray[1]); 
                    if (differentValue > 0)
                    {
                        isSame=false;
                        Reporter.log("--image compare the amount of differences is greater than 0: differentValue is " +differentValue+ ".", true);
                    }
               }
               catch (NumberFormatException ex)
               {
                    //Getting error org.im4java.core.CommandException: inf
                    //returns inf if pics are the same
                    if(howDifferent.contains("inf"))
                    {
                      isSame=true;
                      Reporter.log("--image compare the amount of differences is not greater than 0: inf is returned.", true);
                    }

                }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.