ImageMagick转换在命令行中,但在Java Process运行时不工作

发布于 2025-01-20 01:47:34 字数 998 浏览 4 评论 0原文

我有一个 tif 图像,我试图在其中绘制一个框并同时使用 LZW 压缩来压缩图像。

这是我正在运行的命令,它在 Windows 命令行中运行良好。

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"

当我尝试使用 java 程序执行相同的命令时,我创建了一个图像文件,但文件大小为 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);

这里有什么想法吗?

使用IM版本:ImageMagick-7.1.0 我已经包含了出现错误的测试图像。 zip 文件下载

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2025-01-27 01:47:34

如果您使用数组,则在执行 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();
    }
}

正如 Esther 所说,另一种选择是在命令内的参数之间添加空格而不传递数组。

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();
    }
}

如果 IM4Java jar 可用,则更简单的解决方案如下。

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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文