Search code examples
javagraphicsmagickim4java

Run GraphicsMagick compare command using im4java + gm4java


I am trying to do a GraphicsMagick compare using im4java and gm4java. The GraphicsMagick command I'm using is like this:

gm compare -maximum-error 0 -metric MAE -highlight-style xor -hilight-color red -file C:/output/diffFile.pdf C:/input/file1.pdf C:/input/file2.pdf

I'm trying to translate that into Java. I know that im4java was originally built for ImageMagick and their commands may differ. Is it possible to run the above compare using im4java plus gm4java?

I've tried this:

SimpleGMService service = new SimpleGMService();
service.setGMPath("C:/path/to/graphicsMagick/gm.exe");

try
{
    GMConnection connection = service.getConnection();
    try {
        GMBatchCommand command = new GMBatchCommand(service, "compare");
        // create the operation, add images and operators/options
        IMOperation op = new IMOperation();
        op.metric("MAE");
        op.addRawArgs("-file C:/output/diffFile.pdf");
        op.addImage();
        op.addImage();

        ArrayListOutputConsumer output = new ArrayListOutputConsumer();
        command.setOutputConsumer(output);

        //debug
        command.createScript("C:/output/myscript.bat",op);

        command.run(op, "C:/input/file1.pdf", "C:/input/file2.pdf");

....

The above gives me the error:

org.im4java.core.CommandException: compare: Unrecognized option (-file C:/output/diffFile.pdf)

Solution

  • You can attain this by using im4java alone or im4java+gm4java. What gm4java gives you is the performance when you need to process a large number of images.

    The problem you are getting is due to the improper use of the addRawArgs() method. Each argument you have in your command line needs to be added as individual arguments instead of all together in one string.

    Try:

    op.addRawArgs("-file", "C:/output/diffFile.pdf");