Complex Imagemagick command via ProcessBuilder

可紊 提交于 2019-12-25 08:37:59

问题


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

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.

  • Escape paths
  • Remove PowerShell escape character ``
  • Split the command in single array items (e.g. "(", "+clone", "-alpha")

What do I miss?


回答1:


What you miss is that each parameter should indeed be a single string of its own, something like:

commands.add("(");
commands.add("+clone");
commands.add("-alpha");
commands.add("extract");
commands.add(maskFile.getAbsolutePath());
commands.add("-compose");
commands.add("Darken");
commands.add("-composite");
commands.add(")");

Since there is no shell involved, each string is passed verbatim to the called executable, so you don't need neither escapes nor quotes or backquotes.




回答2:


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

I do not think this is a valid command. You cannot have -alpha without a value for it. Perhaps you wanted -alpha extract?




回答3:


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.

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

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


来源:https://stackoverflow.com/questions/44760382/complex-imagemagick-command-via-processbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!