0

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

cmac
  • 203
  • 2
  • 3
  • 9
  • what errors are you getting? – Esther Apr 08 '22 at 17:53
  • also, you should be passing the entire command line in as one string with spaces, not as an array of strings – Esther Apr 08 '22 at 17:54
  • you can also use getErrorStream, which gives you an InputStream containing stderr of your process, see https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html – Esther Apr 08 '22 at 17:56
  • You've got two commands there. Why?. You need to read stderr (or maybe redirect it and read stdout). Those paths don't need quoting – g00se Apr 08 '22 at 18:25
  • The commandPNG is for another process I should have excluded it from the post – cmac Apr 08 '22 at 19:40
  • See this post. They suggest using an string array for image imagick https://stackoverflow.com/questions/2148043/problem-with-imagemagick-and-java-runtime-exec – cmac Apr 08 '22 at 19:42

1 Answers1

1

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();
    }
}
burnema
  • 26
  • 3