Page 1 of 1

Crop coordinates to text file

Posted: 2016-02-26T12:08:57-07:00
by hrvsmario
Hi. I'm using Imagemagik for Windows. I need to resize and crop several images so I've created a batch file. The resulting images are perfect, but I'm wondering if there is any way to get the x1,y1,x2,y2 coordinates of each crop. I'm using -gravity center so what I need is to log to a text file the coordinates of each file croped. Any help will be most appreciated! thank you!

Re: Crop coordinates to text file

Posted: 2016-02-26T14:12:29-07:00
by fmw42
See %@ string format at http://www.imagemagick.org/script/escape.php

convert image -format "%@" info:

gives the crop arguments without having to crop the image.

Alternately, crop the imaged but do not add +repage. Then get the crop arguments from %O and %wx%h.

You will have to use fx calculations to convert ul coordinates and size to ul and br coordinates.

Please always provide your IM version when asking questions.

Re: Crop coordinates to text file

Posted: 2016-02-26T14:49:33-07:00
by snibgo
I think the OP is using "-gravity center -crop..." rather than "-trim", like this (Windows BAT syntax):

Code: Select all

convert ^
  in.png ^
  -gravity center -crop 100x100+0+0 ^
  -format "%%w %%h %%O" +write info: ^
  +repage ^
  out.png >>logfile.txt

Re: Crop coordinates to text file

Posted: 2016-02-26T17:51:17-07:00
by GeeMack
snibgo wrote:I think the OP is using "-gravity center -crop..." rather than "-trim", like this (Windows BAT syntax):

Code: Select all

convert ^
  in.png ^
  -gravity center -crop 100x100+0+0 ^
  -format "%%w %%h %%O" +write info: ^
  +repage ^
  out.png >>logfile.txt
In order to append that information to "logfile.txt" I have to put a newline "\n" in the formatting operator. Otherwise it just keeps adding the new information to the existing line. Also some other information might be helpful in the log, like the name of the original file, and maybe its size before cropping. That could easily be done with a little tweak to your suggestion using the "%f" to provide the file name and the "%G" to provide the pre-crop dimensions, something like this...

Code: Select all

...
  -format "%f: %G %O %w %h\n" +write info: ^
...

Re: Crop coordinates to text file

Posted: 2016-02-26T18:05:10-07:00
by snibgo
True. Thanks.

Re: Crop coordinates to text file

Posted: 2016-02-27T03:09:05-07:00
by hrvsmario
Thank you very much for your help, I really appreciate it.