0

Any help is very valuable for me.

I am trying to generate a polygon using imagemagick. I am passing imagemagick query to

Runtime.getRuntime().exec(query)

but its not working.

Below is the detail of steps followed by me

getting correct result executing below query directly in commandline in CentOS:

convert -size 100x60 xc:skyblue -fill white -stroke black -draw " polygon 40,10,20,50,90,10,70,40 " draw_polygon.gif

when i am using java's in window machine, again it is giving correct output. Runtime.getRuntime().exec(query);

But when i am using same java code in centOS then it is giving error.

public class TestIMPoly {

    public static void main(String[] args) {
        try {
        String query = "convert -size 100x60 xc:skyblue -fill white -stroke black -draw \" polygon 40,10,20,50,90,10,70,40 \" draw_polygon.gif";
            System.out.println("query : "+query);
            Process  p = Runtime.getRuntime().exec(query);

            // below part is used for determing if errro occured or success (this part is working correct)
            if(p != null) {
                BufferedReader stdInput = new BufferedReader(new 
                         InputStreamReader(p.getInputStream()));

                    BufferedReader stdError = new BufferedReader(new 
                         InputStreamReader(p.getErrorStream()));

                    // read the output from the command
                    System.out.println("Here is the standard output of the command:\n");
                    String s = null;
                    while ((s = stdInput.readLine()) != null) {
                        System.out.println(s);
                    }

                    // read any errors from the attempted command
                    System.out.println("Here is the standard error of the command (if any):\n");
                    while ((s = stdError.readLine()) != null) {
                        System.out.println(s);
                    }
            }
            try {
                p.waitFor();
                System.out.println(p.exitValue());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // below part is used for printing the success message or error message (this part is also working correct)
            InputStream  is = p.getInputStream();
            if (null != is) {
                int size = is.available();
                if (size > 0) {
                    byte[] bytes = new byte[size];
                    int numread  = is.read(bytes);
                    System.out.println("numread : "+numread);
                    if (numread != 0) {
                        String msg = new String(bytes);
                        System.out.println("msg : "+msg);
                        if (null != msg && msg.length() > 0) {
                            String[] params = msg.split(" ");
                            System.out.println("null != params"+(null != params));
                            if (null != params && params.length > 0) {
                                System.out.println();
                                System.out.println(params[2]);
                                System.out.println();
                                is.close();
                                int width = Integer.parseInt(params[2].substring(0, params[2].indexOf("x")));
                                int height = Integer.parseInt(params[2].substring(params[2].lastIndexOf("x") + 1));
                                System.out.println("width :"+width);
                                System.out.println("height :"+height);
                            }
                        }
                    }
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

}

if i am using query

convert -size 100x60 xc:skyblue -fill white -stroke black -draw "polygon  40,10 20,50 90,10 70,40"   draw_polygon.gif

then the error message i am getting as below (same query is working fine in executing directly in commandline in centos):

convert: unable to open image '40,10': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '20,50': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '90,10': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '70,40"': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
1

if i am using query

convert -size 100x60 xc:skyblue -fill white -stroke black -draw " polygon  40,10,20,50,90,10,70,40 "   draw_polygon.gif

then the error message i am getting as below (same query is working fine in executing directly in commandline in centos):

convert: unable to open image 'polygon': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '40,10,20,50,90,10,70,40': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '"': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
1

=============================

Here is the imagemagick convert -version output form commandline as well as using

Process  p = Runtime.getRuntime().exec("convert -version");

output:

Version: ImageMagick 7.0.7-5 Q16 x86_64 2017-10-01 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib cairo djvu fftw fontconfig fpx freetype gslib jng jpeg lcms ltdl lzma pangocairo png ps tiff webp wmf x xml zlib

As this is marked duplicate,

i checked Similar Question and tried accordingly but still i am getting almost similar result, i want to add another scenario as below :

The query which is working fine from commandline (to make corners round shape):

convert D:\img-query\complex\tect.jpg (
 +clone  
 -alpha extract 
 -draw "fill black polygon 0,0 0,20 20,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

But when i am using every arguments as separate string, even then i am getting error. below is the java code used by me,

List<String> commands = new ArrayList<>();
            commands.add("D:/software/ImageMagick-7.0.5-2-portable-Q16-x64/convert.exe");
            commands.add(reSizedCoverBefor3D); 
            //commands.add("(");
            commands.add("+clone");  
            commands.add("-alpha");
            commands.add("extract"); 
            commands.add("-draw");
            //commands.add("fill"); 
            //commands.add("black"); 
            commands.add("polygon"); 
            commands.add("0,0 0,20 20,0"); 
//          commands.add("0,50"); 
//          commands.add("50,0"); 
            commands.add("fill"); 
            commands.add("white"); 
            commands.add("circle"); 
            commands.add("50,50 50,0");
            //commands.add("50,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(roundaedImageFullPath);
            ProcessBuilder pb = new ProcessBuilder(commands);
             pb.inheritIO();
                try {
                    int j = pb.start().waitFor();
                    System.out.println("Finished with code: " + j);
                } catch (Exception e) {
                    System.out.println("asdasdasd: " + e);
                }

Here is error message:

convert: unable to open image 'black': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image 'polygon': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '0,0 0,20 20,0': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image 'fill': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image 'white': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image 'circle': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '50,50 50,0': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
convert: unable to open image '50,0': No such file or directory @ error/blob.c/OpenBlob/3146.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
Finished with code: 1
madhepurian
  • 271
  • 1
  • 13
  • Don't use `exec` with a single string. It doesn't parse the line the same way as the shell does. You have to separate the parameters yourself, and pass them in an array, removing the quotes (which are only for the shell, but keeping the spaces. – RealSkeptic Aug 13 '19 at 09:37
  • it probably has to do with the quote - so try any combination, ' ", remove spaces etc – gpasch Aug 13 '19 at 09:41
  • Thanks @RealSkeptic i will try according to your suggestions – madhepurian Aug 13 '19 at 10:12
  • I checked similar questions and find answers on how to not use entire query as single command but to use separate string for each argument and its value. Even then those suggestions are not enough to help many times so people answer / advice again with some suggestions based on the complexity of the query. Please un mark this as duplicate so that i can get some solution – madhepurian Aug 14 '19 at 21:25

0 Answers0