Beginner - create rectangle splitted into few another rectagles

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
lazna
Posts: 7
Joined: 2011-11-21T06:34:18-07:00
Authentication code: 8675308

Beginner - create rectangle splitted into few another rectagles

Post by lazna »

Scripting network monitor system, want to generate rectangles with few colored "sub-areas", each area should be filled by text from variable. Something like http://diag.pvfree.net/db.png.

after many tries, have constructed this:

convert.exe -size 100x60 -background %back_color% -fill none -stroke %line_color% -draw "rectangle 4,2 96,20" -pointsize %text_size% label:%text_var% %outfile%.gif

but unable to go ahead. Adding another '-draw' section give me mostly "convert.exe: no images defined `output.gif' @ error/convert.c/ConvertImageCommand/3212."

Could someone put me on right direction?

Thanks, L.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Beginner - create rectangle splitted into few another rectagles

Post by snibgo »

Your command:

Code: Select all

convert.exe ^
  -size 100x60 ^
  -background %back_color% -fill none -stroke %line_color% ^
  -draw "rectangle 4,2 96,20" ^
  -pointsize %text_size% label:%text_var% ^
  %outfile%.gif
... has the arguments in the wrong order. First, create the image at the size you want. Then draw on it. Finally, save it.

Code: Select all

convert.exe ^
  -size 100x60 ^
  -background %back_color% -fill none -stroke %line_color% ^
  -pointsize %text_size% label:%text_var% ^
  -draw "rectangle 4,2 96,20" ^
  %outfile%.gif
snibgo's IM pages: im.snibgo.com
lazna
Posts: 7
Joined: 2011-11-21T06:34:18-07:00
Authentication code: 8675308

Re: Beginner - create rectangle splitted into few another rectagles

Post by lazna »

Thanks, but still does not understand elements order logic:

2nd line define parent square size:

-size 100x60 ^

3th line define parent square background color, "-fill" is background color for child square, "-stroke" is outline color of childsquare.

-background %back_color% -fill none -stroke %line_color% ^

4th line defined size of text and text itself

-pointsize %text_size% label:%text_var%

5th line draw child square itself

-draw "rectangle 4,2 96,20"

What If I want to add another empty sub-rectangle below the created one? Simply repeating lines 3-5 as lines 6-8 does not work. I still does not understand command order the convert.exe expect :-/

Could someone clarify it for me, please?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Beginner - create rectangle splitted into few another rectagles

Post by Bonzo »

The code supplied by yourself and corrected by snibgo just creates one box containing the text and border.

You say you want "something like" the attached file - how alike?

Where is the position of the box on the large image given?

I think you need to create the large background image and create the boxes with text and place them on the background as you go using -composte or -page. This will need a script to loop through all the boxes with the text and location which then creates the small image and places it on the background image. If you did no use lable but -annotate it might work.

If you want a "stack" of the small images you can use -append but I do not know if you can do it all on one line and may have to still create each box before "stacking them".
lazna
Posts: 7
Joined: 2011-11-21T06:34:18-07:00
Authentication code: 8675308

Re: Beginner - create rectangle splitted into few another rectagles

Post by lazna »

I am interesting of how to create one small green box from page I post as example. Rest I can do myself by shell scripting. Do not understood the "language" I have to tell to convert.exe following:

Create white area sized x/y, inside this area create rectangle sized x/y, positioned by offset x/y, filled with no color (inherit white from parent area), and into this rectangle put text "xyz". Than in same parental area (but out of previously created rectangle) create another rectangle sized x/y, positioned by offset x/y, and contain text "abc".

The code samples I posted earlier are derived from examples in documentation, but read whole documentation is out of my possibilities (too detailed) :-/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Beginner - create rectangle splitted into few another rectagles

Post by Bonzo »

From what I remember the draw and text get corrupted as the draw sets the location for it and the text. I think there is a way around the problem but I can not remember what it is.

This is why I suggested drawing the rectangles with the text separately and then compositing them over the background image with a script.

So back to your question:
Background:
convert -size 500x500 xc:white background.jpg
Text box ( canvas 100px wide 60px high, canvas transparent, text in the center of the canvas , text colour black, point size of 8, write the text abc x0 and y0 offset )
convert -size 100x60 xc:none -gavity center -fill black -pointsize 8 -annotate +0+0 "abc" text.png
Position the box on the image
convert background.jpg text.png -page 100x100 -composite output.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Beginner - create rectangle splitted into few another rectagles

Post by fmw42 »

bonzo wrote:convert background.jpg text.png -page 100x100 -composite output.jpg
I am surprised that -page works with -composite. I thought it needed -flatten and needs to come before the second image (or use -set page if after the second input) and -composite would use -geometry. Either way it should be +100+100

Code: Select all

convert background.jpg -page +100+100 text.png -flatten output.jpg
or

Code: Select all

convert background.jpg text.png -geometry +100+100 -composite output.jpg
lazna
Posts: 7
Joined: 2011-11-21T06:34:18-07:00
Authentication code: 8675308

Re: Beginner - create rectangle splitted into few another rectagles

Post by lazna »

convert.exe -size 500x500 xc:white -stroke black -fill none -draw "rectangle 4,2 96,50" background.png & convert.exe -size 100x60 xc:
none -gravity center -fill black -pointsize 16 -annotate +0+0 "Hallo world" text.png & convert.exe background.png text.png -page 100x100 -composite output.png

Finaly, this command create white area with small rectangle containing text "hallo world". (probably) Last question is how do I add another small rectangle next first one? Trying to duplicate "-draw" section from first command but it does not work.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Beginner - create rectangle splitted into few another rectagles

Post by snibgo »

You have three commands, not one. The first creates background.png, with a box where the text will be. The second creates text.png. The third combines the two image.

You could keep repeating the the second and third commands, with more "-draw" commands as needed.

I would probably do it something like this (Windows BAT syntax):

Code: Select all

convert ^
  -pointsize 20 ^
  -background #0f0 -fill #f0f ^
  -bordercolor Black ^
  -gravity Center ^
  ( label:"First box" -border 1 -set page +20+30 ) ^
  ( label:"Second box" -border 1 -set page +100+0 ) ^
  ( label:"Third box" -border 1 -set page +200+300 ) ^
  -background #88f ^
  -layers merge ^
  m1.png
or:

Code: Select all

convert ^
  -size 120x30 ^
  -pointsize 20 ^
  -background #0f0 -fill #f0f ^
  -bordercolor Black ^
  -gravity Center ^
  ( label:"First box" -border 1 -set page +20+30 ) ^
  ( label:"Second box" -border 1 -set page +100+0 ) ^
  ( label:"Third box" -border 1 -set page +200+300 ) ^
  -background #88f ^
  -layers merge ^
  m2.png
snibgo's IM pages: im.snibgo.com
lazna
Posts: 7
Joined: 2011-11-21T06:34:18-07:00
Authentication code: 8675308

Re: Beginner - create rectangle splitted into few another rectagles

Post by lazna »

Hey snibgo... do you know you are the best??? Thanks a lot!
Post Reply