Page 1 of 1

Trim image and output trim amount

Posted: 2018-06-30T07:03:14-07:00
by Oli414
I'm trying to trim an image and output the trim amount,

So far I'm testing with:

Code: Select all

magick in.png -trim out.png
But I'm already getting stuck when trying to both output some info, and output an image in the same command:

Code: Select all

magick in.png -trim -format "Hello World" info:- out.png
Which won't work. Imagemagick's process won't exit.

How can I get both some text as output and an image saved in the same command? What variables would I then need to get the amount which was trimmed?

Version: 7.0.8-3 Q16
Right now I'm testing on Windows in a CMD window, but in the end, I'll be using subprocess.check_output in Python to get the info I need.

Re: Trim image and output trim amount

Posted: 2018-06-30T07:11:30-07:00
by snibgo
To write info at any stage in a command, use "-write info". And you can "-write out.png" at any stage. The final output mustn't have "-write". So if you want to write info and an image:

Code: Select all

magick in.png -trim -write info: out.png
... or...

Code: Select all

magick in.png -trim -write out.png info:
If you want the trim data in a variable, you must use a shell facility. You don't say what shell you are using: bash, Windows CMD, Windows BAT, or whatever.

In your command with "info:-", IM interprets this as needing "info:" type data as input from stdin, so it is waiting to receive it. That's why it didn't complete.

Re: Trim image and output trim amount

Posted: 2018-06-30T07:33:12-07:00
by Oli414
Thanks, that's all I needed!

Code: Select all

magick in.png -trim -format "%[fx:page.width/2 - page.x] %[fx:page.height/2 - page.y]" -write info: out.png
Allows to get me the untrimmed image's center within the newly trimmed image.

Re: Trim image and output trim amount

Posted: 2018-06-30T08:17:49-07:00
by GeeMack
Oli414 wrote: 2018-06-30T07:33:12-07:00Allows to get me the untrimmed image's center within the newly trimmed image.
You can also get the trim information without actually doing the trim with a command like this...

Code: Select all

magick input.png -set page %[@] output.png
If the output format supports it, like PNG and TIF, the paging information of those FX variables "page.height", "page.width", "page.x", and "page.y" will be as if you trimmed it and be available in the output image while the image retains its original input dimensions. This can be helpful if you want to calculate for a particular "-shave" or "-chop" without trimming the entire image.