通过ProcessBuilder的复杂Imagemagick命令 [英] Complex Imagemagick command via ProcessBuilder

查看:139
本文介绍了通过ProcessBuilder的复杂Imagemagick命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java ProcessBuilder中使用以下ImageMagick命令:

I'm trying to get following ImageMagick command working with the Java ProcessBuilder:

convert.exe image.png `( `+clone -alpha extract mask.png -compose Darken -composite `) -compose CopyOpacity -composite out.png

文件路径(源图像,蒙版图像和目标图像)是可配置的. 如果我在PowerShell或Windwos Cmd中输入命令,则它会按预期工作. 当我尝试通过Java ProcessBuilder执行相同的命令时,它将失败.

The file paths (source image, mask image and destination image) are configurable. If I enter the command in PowerShell or Windwos Cmd it is working as expected. When I'm trying to execute the same command via Java ProcessBuilder, it fails.

这是我的最后一个代码:

Here is my last code:

    File srcFile = new File("C:/Users/AAA/Desktop/PNG/image.png");
    File maskFile = new File("C:/Users/AAA/Desktop/PNG/mask.png");
    File destFile = new File("C:/Users/AAA/Desktop/PNG/out-1.png");

    List<String> commands = new ArrayList<>();
    commands.add("C:/Program Files/ImageMagick-6.9.1-Q16/convert.exe");
    commands.add(srcFile.getAbsolutePath());
    commands.add(" `( `+clone -alpha extract " + maskFile.getAbsolutePath() + " -compose Darken -composite `)");
    commands.add("-compose CopyOpacity -composite " + destFile.getAbsolutePath());

    ProcessBuilder pb = new ProcessBuilder(commands);
    pb.inheritIO();
    try {
        int i = pb.start().waitFor();
        System.out.println("Finished with code: " + i);
    } catch (Exception e) {
        System.out.println("asdasdasd: " + e);
    }

这是流程构建者的经验:

And this is the ourput from the process builder:

convert.exe: unable to open image `/Users/AAA/Desktop/PNG/mask.png -compose Darken -composite )': No such file or directory @ error/blob.c/OpenBlob/2692.
convert.exe: no decode delegate for this image format ` ( +CLONE -ALPHA EXTRACT C' @ error/constitute.c/ReadImage/501.
convert.exe: missing an image filename `-compose CopyOpacity -composite C:\Users\AAA\Desktop\PNG\out-1.png' @ error/convert.c/ConvertImageCommand/3214.

似乎命令的解释方式不正确

It seems that the commands aren't interpreted in the right way

我尝试了以下可能性,但大多数结果都相同.

I have tried following possibilities, but most of them with the same outcome.

  • 转义路径
  • 删除PowerShell转义字符``
  • 将命令分割为单个数组项(例如(","+ clone",-alpha")

我想念什么?

推荐答案

我知道来晚了,但我的回答仍然可以帮助某人.

I know it is bit late, but my answer can still help someone.

我正面临类似的问题处理复杂的查询.

i was facing similar problem with complex queries.

对于具有一个或多个花括号和多个操作的复杂查询,将每个参数及其值作为单独的字符串嵌入将始终无济于事.

In case of complex queries having one or more braces and multiple operations, embedding each argument and its values as separate string wont help always.

我有一个复杂的查询,可以通过以下方式从Java执行, Imagemagick查询:

i had one complex query, which i was i able to execute from java as below, Imagemagick query:

convert D:\img-query\complex\tect.jpg (
 +clone  
 -alpha extract 
 -draw "fill black polygon 0,0 0,50 50,0 fill white circle 50,50 50,0" 
 ( +clone -flip ) 
 -compose Multiply   
 -composite ( +clone -flop ) 
 -compose Multiply 
 -composite 
 ) 
 -alpha off 
 -compose CopyOpacity 
 -composite  D:\img-query\complex\round.png

正在工作的Java代码(在window cmd和centOS中都可以工作):

Working java code (working in window cmd as well as in centOS):

try {
            List<String> commands = new ArrayList<>();
            commands.add("D:/img-query/complex/tect.jpg");
            commands.add(reSizedCoverBefor3D); 
            commands.add("(");
            commands.add("+clone");  
            commands.add("-alpha");
            commands.add("extract"); 
            commands.add("-draw");  
            commands.add("fill black polygon 0,0 0,2 2,0 fill white circle 2,2 2,0");
            commands.add("(");
            commands.add("+clone"); 
            commands.add("-flip");
            commands.add(")");
            commands.add("-compose"); 
            commands.add("Multiply") ;
            commands.add("-composite"); 
            commands.add("(");
            commands.add("+clone"); 
            commands.add("-flop");
            commands.add(")") ;
            commands.add("-compose"); 
            commands.add("Multiply") ;
            commands.add("-composite");
            commands.add(")");  
            commands.add("-alpha"); 
            commands.add("off");
            commands.add("-compose"); 
            commands.add("CopyOpacity"); 
            commands.add("-composite"); 
            commands.add(D:/img-query/complex/round.png");
            ProcessBuilder pb = new ProcessBuilder(commands);
             pb.inheritIO();
                try {
                    Process p = pb.start();
                    int j = p.waitFor();
                    int exitValue = p.exitValue();
                    System.out.println("Finished with code: " + j);
                    System.out.println("Finished with exitValue: " + exitValue);
                } catch (Exception e) {
                    System.out.println("asdasdasd: " + e);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }

这篇关于通过ProcessBuilder的复杂Imagemagick命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆