cmac
cmac

Reputation: 213

imagemagick Convert working in Command line but not in java process runtime

I have a tif image where I am trying to draw a box and compress the image with LZW compression simultaneously.

this is the command I am running, and it works fine from windows command line.

C:\ImageMagick-7.1.0\convert.exe "C:\Users\admin\Orig.tif" -draw "rectangle 576,1069,943,1114" -compress LZW "C:\Users\admin\DrawLZW.tif"

when I try to execute the same command with my java program, I get an image file created, but the file size is 1kb

        String[] cmd = {"C:\\ImageMagick-7.1.0\\convert.exe", "\"C:\\Users\\chris.macwilliams\\Orig.tif\"", "-draw", "\"rectangle 576,1069,943,1114\"", "–compress","LZW", "\"C:\\Users\\chris.macwilliams\\DrawLZWwithJava.tif\""};
        LOGGER.info(cmd);
        Process pt = Runtime.getRuntime().exec(cmd);
        pt.waitFor();

        if (pt.exitValue() != 0) {
            LOGGER.error("ERROR with Image Magic Command exit value:" + pt.exitValue()+  " "+ commandTIF);

Any Ideas here?

Using IM Version: ImageMagick-7.1.0 I have included the test images that I am getting the errors on. zip file download

Upvotes: 0

Views: 674

Answers (1)

burnema
burnema

Reputation: 26

If you are using an array, it's easier to declare the size of the array and add each argument before executing the cmd.

private void redactCMDArray() {
    String[] cmd = new String[7];
    cmd[0] = "C:\\ImageMagick-7.1.0\\convert.exe";
    cmd[1] = "\"C:\\Users\\Administrator\\Desktop\\images\\Orig.tif\"";
    cmd[2] = "-draw";
    cmd[3] = "rectangle 576,1069,943,1114";
    cmd[4] = "-compress";
    cmd[5] = "LZW";
    cmd[6] = "\"C:\\Users\\Administrator\\Desktop\\images\\DrawLZW_CMD_Option1.tif\"";
    System.out.println(Arrays.toString(cmd));
    Process pt;
    try {
        pt = Runtime.getRuntime().exec(cmd);
        pt.waitFor();
        if (pt.exitValue() != 0) System.out.println("ERROR with Image Magic Command exit value:" + pt.exitValue()+  " " + Arrays.toString(cmd));
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

Another option, as stated by Esther, is to add a whitespace between parameters within the command and not pass an array.

private void redactCMDLine(){
    String imPath = "C:\\ImageMagick-7.1.0\\convert.exe";
    String imEXE = "/convert.exe";

    String cmd = imPath + imEXE + " " + "C:\\Users\\Administrator\\Desktop\\images\\Orig.tif" + " " + "-draw \"rectangle 576,1069,943,1114\"" + " " + "-compress LZW"  + " " + "C:\\Users\\Administrator\\Desktop\\images\\DrawLZW_CMD_Option2.tif";
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        System.out.println("Exit code: " + p.exitValue());
    } catch (InterruptedException | IOException e) {
        e.printStackTrace();
    }
}

If the IM4Java jar is available, a more straightforward solution would be the following.

private void redactIM4Java() {
    ConvertCmd convertCmd = new ConvertCmd();
    IMOperation op = new IMOperation();
    op.addImage("C:\\Users\\Administrator\\Desktop\\images\\Orig.tif");
    op.fill("Black");
    op.draw("rectangle 576,1069,943,1114");
    op.compress("LZW");
    op.format("TIF");
    op.addImage("C:\\Users\\Administrator\\Desktop\\images\\DrawLZW_MB_JavaIM4Java.tif");
    try {
        convertCmd.run(op);
    } catch (IOException | InterruptedException | IM4JavaException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions