Add Auto sized text to rectangle area of 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?".
kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-05T15:08:56-07:00

Hi all,

I have been pulling my hair out trying to do this but just could not figure it out.

What I want to do is quite simple and I am sure the answer is simple (if you know!).

I have images which I want to add text to in a box on the right of the images. e.g. the image size is 1000 pixels wide and I want to add a box 200 pixels wide to the right of that, containing some text (of variable length), whereby the text is automatically made as large as possible to fit within that box.

The resulting image would then be 1200 pixels side.


Thats it!

snibgo
Posts: 10929
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add Auto sized text to rectangle area of image

Post by snibgo » 2018-12-05T15:13:34-07:00

What version of IM on what platform?

How high would the output be?
snibgo's IM pages: im.snibgo.com

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

Re: Add Auto sized text to rectangle area of image

Post by fmw42 » 2018-12-05T16:34:52-07:00

Please, always provide your IM version and platform when asking questions, since syntax may differ.

Also provide your exact command line and your images, if possible.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
https://imagemagick.org/script/porting.php#cli

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T08:24:23-07:00

Thanks and sorry for not posting version etc.
Version I'm running is 7.0.8-14 Q16 x64 2018-10-24

I have tried following the advice and examples but I am obvisouly doing something wrong. I have got it to draw a rectangle and add some test, but I cant figure out how to make that text resize itsself so it fits the rectangle...

magick convert testing2.png -fill "#285EA6" -draw "rectangle 889,0,1200,627" -fill white -annotate +895+304 "This is some text that could be of any length which should be made as big as possible to fill the box" testing2-out.png

The output is no different to the input.

snibgo
Posts: 10929
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add Auto sized text to rectangle area of image

Post by snibgo » 2018-12-06T08:37:59-07:00

You didn't answer my question: How high would the output be?

The obvious method is to make an image of just the text, and append that to the side of the input image.

For v7, I suggest you use "magick", not "convert" or "magick convert".

Suppose the image is 800 pixels high, and you want the text to be that height, and 200 wide:

Code: Select all

magick -size 200x800 caption:"This is some text that could be of any length which should be made as big as possible to fill the box" out.png
If that does what you want, then you can use that in a longer command that reads the input image, uses its height for the text, and appends the two images.
snibgo's IM pages: im.snibgo.com

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T08:54:27-07:00

Ahh sorry - it will be as high as the original image.

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T09:01:56-07:00

OK - so...
magick testing2.png -size 200x800 caption:"This is some text that could be of any length which should be made as
big as possible to fill the box" out.png

the indeed does create a box and fills with text - great - but now the original image is lost.

So if the original image is 800x800 and the text box created is 200x800 i want an output image which is 1000x800. ie.e the original image with the text in a box added to the right.

(Many thanks for helping by the way)

snibgo
Posts: 10929
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add Auto sized text to rectangle area of image

Post by snibgo » 2018-12-06T09:18:44-07:00

And (for the third time) what is your platform? I'll assume Windows BAT.

Code: Select all

magick ^
  in.png ^
  -size 200x%%[fx:h] ^
  caption:"This is some text that could be of any length which should be made as big as possible to fill the box" ^
  +append ^
  out.png
snibgo's IM pages: im.snibgo.com

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T09:28:28-07:00

yes, windows 10 bat - sorry i missed that question

The last cmd you sent does adds 47 pixels to the righ of my input image, and does not scale the text to fit...

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T09:45:46-07:00

I have looked in the docs https://www.imagemagick.org/script/comm ... s.php#size to understand what

-size 200x%%[fx:h

does, but it doesn't explain the %% or the parameters in square brackets...

snibgo
Posts: 10929
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add Auto sized text to rectangle area of image

Post by snibgo » 2018-12-06T09:51:21-07:00

"200" is the required width in pixels. "%[fx:h]" is the required height in pixels. The expression uses the height from the image that is already in the list. For BAT we need to double the %.

It works fine for me with IM v7.0.7-28. I suggest you post a link to your input image.
snibgo's IM pages: im.snibgo.com

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T11:18:00-07:00

Many many thanks for continuing to help me.

Just came back to check - only to find my post with links to the input and output images is not here!

Just one of those days!...

input
http://peaf.com/images/testing2.png

output
http://peaf.com/images/out.png

command executed
magick ^
testing2.png ^
-size 200x627[fx:h] ^
-fill "#285EA6" -draw "rectangle 889,0,1200,627" ^
caption:"This is some text that could be of any length which should be made as big as possible to fill the box" ^
+append ^
out.png

(I wanted to also change the background color to a blue but its been applied to the text.)

As you can see - it hasn't added 200 pixels to the right and the text is not sized to fit.

snibgo
Posts: 10929
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add Auto sized text to rectangle area of image

Post by snibgo » 2018-12-06T11:45:58-07:00

Your size setting is different to mine. Yours isn't valid syntax.

Your image testing2.png is only 888 pixels wide, so drawing a rectangle starting at 889,0 won't do anything.

If you want a coloured bckgound to the text, I suggest you use "-background", eg:

Code: Select all

%IMG7%magick ^
  testing2.png ^
  -size 200x%%[fx:h] ^
  -background #285EA6 ^
  caption:"This is some text that could be of any length which should be made as big as possible to fill the box" ^
  +append ^
  capt-right.png
Image
snibgo's IM pages: im.snibgo.com

kevinleesmith
Posts: 14
Joined: 2014-10-17T03:12:47-07:00
Authentication code: 6789

Re: Add Auto sized text to rectangle area of image

Post by kevinleesmith » 2018-12-06T11:55:45-07:00

Hmm thanks for that, I copied and pasted the command and I get
As you say my input image is 888 wide and so i need magick to add 200 pixels to the right as you show in your image.

Image

snibgo
Posts: 10929
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Add Auto sized text to rectangle area of image

Post by snibgo » 2018-12-06T12:04:29-07:00

I said I assume you are running this as a BAT script, which is why % needs to be doubled to %%.

I suspect you are not running this as a BAT script, but have still doubled the %%.
snibgo's IM pages: im.snibgo.com

Post Reply