get image size with graphicsmagick

2.3k Views Asked by At

for one of my project i am using im4java library and send command through it to graphicsmagick.

to get image size :

identify -format \"%w,%h,\"  test.png

i can get size properly till the following result. this time stderr changes the result:

[wx=0.345750,  wy=0.358550,  rx=0.648500,  ry=0.330880
gx=0.321210,  gy=0.597870,  bx=0.155890,  by=0.066040
163, 70, 
identify: Ignoring incorrect cHRM value when sRGB is also present ]

is there any way to get just image size and ignore stderr result?

thanks in advance

3

There are 3 best solutions below

0
On

Do the following:

identify -format \"width=%w,height=%h,\" test.png

Then parse the result specifically looking for width=N and height=N

0
On

// For me "ping" is the fastest way to get the dimension.

        String resultFormat = "%w" + " " + "%h";
        ArrayListOutputConsumer outputConsumer = new ArrayListOutputConsumer();

        IdentifyCmd identify = new IdentifyCmd();

        identify.setOutputConsumer(outputConsumer);
        IMOperation op = new IMOperation();
        op.ping();
        op.format(resultFormat);
        op.addImage("abc.jpg");

        //Object[] arguments = new String[]{filePath};

        try {
                identify.createScript("c:\\_work\\temp\\magick-dimension.sh.txt", op);  // this will show you the actual im4java generated command
                identify.run(op);

        } catch (IOException | InterruptedException | IM4JavaException e) {
                //throw new ImageAccessException(e.getMessage(), e);
                e.printStackTrace();
        }

        ArrayList<String> output = outputConsumer.getOutput();

        String[] dimensions = output.get(0).split(separator);
        Dimension result = new Dimension(Integer.valueOf(dimensions[0]), Integer.valueOf(dimensions[1]));
        return result;
0
On

Try adding -quiet to your command to suppress the error message, like this:

identify -quiet -format \"%w,%h,\"  test.png