Crop coordinates to text file

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
hrvsmario
Posts: 2
Joined: 2016-02-26T11:58:46-07:00
Authentication code: 1151

Crop coordinates to text file

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop coordinates to text file

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop coordinates to text file

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Crop coordinates to text file

Post 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: ^
...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop coordinates to text file

Post by snibgo »

True. Thanks.
snibgo's IM pages: im.snibgo.com
hrvsmario
Posts: 2
Joined: 2016-02-26T11:58:46-07:00
Authentication code: 1151

Re: Crop coordinates to text file

Post by hrvsmario »

Thank you very much for your help, I really appreciate it.
Post Reply