Draw rectangle w/relative positions rather than absolute?

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
stevepugh
Posts: 43
Joined: 2008-01-21T12:34:36-07:00

Draw rectangle w/relative positions rather than absolute?

Post by stevepugh »

Hi all,

I would like to load an image from disk and draw a rounded rectangle using 40% of the image's width and 40% of the image's height as the upper left corner and 70% of the width, 70% height as the lower right corner of the rectangle.

How would I accomplish this? From what I can read in the usage docs, -draw needs pixel coordinates and these strike me as absolute.

Many thanks, and sorry if this is...lame.

Best,
Steve
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Draw rectangle w/relative positions rather than absolute?

Post by fmw42 »

ulx=`convert xc: -format "%[fx:40*w/100]" info:`
uly=`convert xc: -format "%[fx:40*h/100]" info:`
brx=`convert xc: -format "%[fx:70*w/100]" info:`
bry=`convert xc: -format "%[fx:70*h/100]" info:`

feed these to -draw "roundrectangle $ulx,$uly $brx,$bry ...."

see fx escapes
http://www.imagemagick.org/Usage/transform/#fx_escapes
stevepugh
Posts: 43
Joined: 2008-01-21T12:34:36-07:00

Re: Draw rectangle w/relative positions rather than absolute?

Post by stevepugh »

Wow, just a cursory glance at that link and my eyes are bleeding. I've got to put a pot of coffee on and start digging! Considering that a contact sheet 'montage' operation is as hardcore as I've gotten so far with IM, this is a bit of a jump for me.

To complicate matters, I'm on a WindowsXP box and am frankly not sure that it's got the mad scripting capabilities of bash/perl. I shudder at the thought. Ought this still be doable, ya think?

Many thanks,
Steve
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Draw rectangle w/relative positions rather than absolute?

Post by fmw42 »

The computations of the coordinates for the UL and BR are done using only IM commands (fx escape calculations).
They simple use -fx to get the width (w) or the height (h) and scale by 40/100=40% or 70% and put those in variables that can be used in -draw for the coordinates.

They are not bash scripting computations such as the use of bc or expr. So they should work in Windows. However, there are some Windows considerations you should review. See

http://www.imagemagick.org/Usage/api/#windows
Post Reply