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

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
zoomphoto

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

Post 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!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

Post by Bonzo »

Try:

Code: Select all

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

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

Post 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!!
zoomphoto

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

Post 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
zoomphoto

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

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

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

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply