Page 1 of 1

Looked all through the docs, need to draw a box on an image

Posted: 2010-08-26T10:05:03-07:00
by zoomphoto
I use IM quite a bit on my site, and I am stumped on this one.. maybe I am just not thinking outside the box, but anyway here is what I need to do.

So I have an image thumbnail, 100x150 let's say. And I want to draw a box on that thumbs to represent a 8x10 image. So 100x150 would be a 8x12, I want to draw a box that is roughly 100x130ish centered. This would give people a representation of what an 8x10 print would look like as it's much smaller than the 8x12 original image.

Here is an example of what I mean.

So the thumbnail would look like this:

Image

And when they select to purchase an 8x10, it would look like:

Image

Now ignore how the box looks, it can be a simple line, we don't need to be fancy, the idea here is to prompt the user ordering the 8x10 to choose how they want it cropped... and if this doesn't work for them, they can purchase another size.

Any help would be greatly appreciated!

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T11:03:37-07:00
by Bonzo
Try:

Code: Select all

convert input_image -stroke black -strokewidth 1 -fill none -draw "rectangle 20,20 193,140 " output_image

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T11:16:01-07:00
by zoomphoto
Thanks! We're close, but that gets me close enough to now tweak it and create the command via code to allow for variables...

Image

Thanks!!

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T11:27:48-07:00
by zoomphoto
Taking what you gave me, I changed it up a little and have exactly what I wanted:

Code: Select all

convert input_image -stroke red -strokewidth 2 -fill none -draw "rectangle 0,20 99,130" output_image
Image

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T13:07:52-07:00
by zoomphoto
Just incase anyone else has this same requirement.. I was playing with IM and it's super powerful!

Code: Select all

convert input_image -stroke red -strokewidth 1 -fill white -draw "fill-opacity 0 rectangle 0,20 99,130" -draw "fill-opacity 0.6 stroke-opacity 0 rectangle 0,0 99,20" -draw "fill-opacity 0.6 stroke-opacity 0 rectangle 0,130 130,150" output_image
Image

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T13:24:32-07:00
by Bonzo
Thats looking good; are you using php? If so I now write my code like this as its easier to read and you can echo the contents of $cmd to confirm it contains what you expect.

Code: Select all

$cmd = " input_image -stroke red -strokewidth 1 -fill white ".
 " -draw \"fill-opacity 0 rectangle 0,20 99,130\" ".
 " -draw \"fill-opacity 0.6 stroke-opacity 0 rectangle 0,0 99,20\" " .
 " -draw \"fill-opacity 0.6 stroke-opacity 0 rectangle 0,130 130,150\" ".
 " output_image ";

exec("convert $cmd ");

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T16:49:53-07:00
by fmw42
Here is a slightly different way to do the same, but also computes the location for the red rectangle such that it will always be 8:10 ratio and centered in a portrait mode image.

Crop a copy of the image to get the right 8:10 ratio centered area, trim an amount equal to the strokewidth all around, add a red border of thickness equal to the strokewidth. Then colorize a copy of the original image with white. Then composite the center area back into the colorized image.

Image

width=`convert bicyclist.jpg -format "%w" info:`
height=`convert bicyclist.jpg -format "%h" info:`
ratio=0.8
swidth=2
ww=$width
hh=`convert xc: -format "%[fx:$ratio*$height]" info:`
echo "width=$width; height=$height; ww=$ww; hh=$hh;"
width=100; height=150; ww=100; hh=120;

convert bicyclist.jpg \
\( -clone 0 -fill white -colorize 60% \) \
\( -clone 0 -gravity center -crop ${ww}x${hh}+0+0 +repage \
-shave ${swidth}x${swidth} -bordercolor red -border $swidth \) \
-delete 0 -gravity center -composite bicyclist_red_rect2.jpg

Image

see
-colorize at http://www.imagemagick.org/Usage/color/#colorize
-crop, -shave and -border at http://www.imagemagick.org/Usage/crop/
-compose at http://www.imagemagick.org/Usage/compose/#compose
parenthesis processing and clones at http://www.imagemagick.org/Usage/basics/#parenthesis

Re: Looked all through the docs, need to draw a box on an im

Posted: 2010-08-26T17:49:39-07:00
by anthony
You can also draw the box using dashed lines.
see IM Examples, Drawing
http://www.imagemagick.org/Usage/draw/#mvg

Point. If you just use stroke and fill colors to fill the box, the fill will be used to 'fill' the box.
Only the stroke color will be used as an outline of the box.

to get a black and white dashed line around the box, you may either need to draw the lines twice to get just the line filled with white, or draw the box one line at a time.