使用graphicsmagick获取图像大小

时间:2015-01-05 11:27:24

标签: imagemagick graphicsmagick im4java

对于我的一个项目我正在使用im4java库并通过它发送命令到graphicsmagick。

获取图像尺寸:

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

我可以正确地获得尺寸直到以下结果。这次stderr改变了结果:

[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 ]

有没有办法获得图像大小并忽略stderr结果?

提前致谢

3 个答案:

答案 0 :(得分:0)

执行以下操作:

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

然后解析结果,专门寻找width = N和height = N

答案 1 :(得分:0)

//对我而言,“ping”是获得维度的最快方式。

        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;

答案 2 :(得分:-1)

尝试在命令中添加-quiet以禁止显示错误消息,如下所示:

identify -quiet -format \"%w,%h,\"  test.png
相关问题