Trim image and output trim amount

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Oli414
Posts: 2
Joined: 2018-06-30T06:57:02-07:00
Authentication code: 1152

Trim image and output trim amount

Post by Oli414 » 2018-06-30T07:03:14-07:00

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.

snibgo
Posts: 10562
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim image and output trim amount

Post by snibgo » 2018-06-30T07:11:30-07:00

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.
snibgo's IM pages: im.snibgo.com

Oli414
Posts: 2
Joined: 2018-06-30T06:57:02-07:00
Authentication code: 1152

Re: Trim image and output trim amount

Post by Oli414 » 2018-06-30T07:33:12-07:00

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.

User avatar
GeeMack
Posts: 612
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trim image and output trim amount

Post by GeeMack » 2018-06-30T08:17:49-07:00

Oli414 wrote:
2018-06-30T07:33:12-07:00
Allows 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.

Post Reply