Question

I am using Imagemagick to trim some png files (remove transparent border), I use the command below:

convert -debug Coder longyan_1.png -trim longyan_1.trimed.png

Then the tool gave me a trimed image as I wanted, but I want more information about the trim offset.

In the debug info I can see the line below:

2014-05-10T14:32:42+08:00 0:04.860 4.840u 6.8.7 Coder convert[49089]: png.c/WriteOnePNGImage/10747/Coder
  Setting up oFFs chunk with x=181, y=118, units=0

The "x,y" info is exactly what I want, but the debug info is too much to deal with, is there any Imagick native API or any other tricks to get the offset info?

Was it helpful?

Solution 2

I am sure that there are more complicated ways to do this but, if you just get the offset info, you can use grep:

$ convert -debug Coder longyan_1.png  -trim longyan_1.trimed.png 2>&1 | grep "Setting up oFFs chunk"
   Setting up oFFs chunk with x=181, y=118, units=0

If you want to shorten and/or reformat the output still more, you can use sed. For example:

$ convert -debug Coder longyan_1.png  -trim longyan_1.trimed.png 2>&1 | sed -n '/Setting up oFFs chunk/ s/.*x=/x=/p' 
x=181, y=118, units=0

OTHER TIPS

I think you actually need the %@ trim box info, which you get like this:

convert image.png -format "%@" info:
233x375+60+51

or equally, maybe even more succinctly:

identify -format "%@" image.png
233x375+60+51

So, to test it out, I create an image like this:

convert -size 400x200 xc:red -size 80x40 xc:yellow -gravity center -composite image.png

enter image description here

And then ask ImageMagick how it would trim it:

convert image.png -format "%@" info:
80x40+160+80
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top