0

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?

Xerox23
  • 133
  • 4
  • 7

3 Answers3

0

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.

xenoid
  • 8,396
  • 3
  • 23
  • 49
  • I already tried it (last point of my list). But if I split them, i got follwoing error: `Unzul�ssiger Parameter - (` (I don't know the exact english translation, something like "Invalid/Incorrect parameter - (") – Xerox23 Jun 27 '17 at 12:00
  • Possibly because you are putting "-(" together in a parameter. Hard to diagnose without seeing your actual code. But I stand with my remark, you should use distinct strings. – xenoid Jun 27 '17 at 15:45
  • I used your code snippet. The "-" sign is part of the exception, and not a part of the command list. – Xerox23 Jun 28 '17 at 11:18
  • if you give me a "raw" IM convert command that is supposed to work in a command prompt I will give it a shot. – xenoid Jun 28 '17 at 11:52
0

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?

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

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();
        }
madhepurian
  • 271
  • 1
  • 13