ImageMagick best fit text within rectangle? - text

I have an image like this, with a rectangle at specific coordinates:
(for illustratory purposes I put the coordinates of the rectangle and its size and center in there)
Now I want to render some text with ImageMagick, so that it fits exactly within the rectangle.
If it's a very short (narrow) string, the rectangle's height will be the limiting factor:
On the other hand with a long (wide) string, the rectangle's width will determine the size:
In either case, independent of how short or long the text is, I would like to print it in one line (i.e. no word wrapping or multi line), and have it fit exactly in the rectangle, and make sure it's centered (the center of the text is in the center of the rectangle).
My questons:
How to do this 'best fit' feature with ImageMagick (I don't know how to dynamically determine the required -pointsize for this)
How to get the text centered, when I use -gravity center it seems to apply to the position of the text within the entire image, i.e. text coordinates become relative to the entire image's center. But I want to specify exact (absolute) coordinates, and that should be the center of the text.
For example, if I do this:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 -draw "text 398,90 'Hello'" test2.jpg
I'm getting:
Note how the coordinates I specify (the rectangle's center) become the bottom left anchor point for the text! (this surprised me)
And if I do:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 -gravity center -draw "text 148,-94 'Hello'" test3.jpg
I get:
Which is kinda OK, but note the weird text coordinates I have to use to get that. And besides I wouldn't know how to automatically calculate the pointsize (did the above by trial and error).

Updated Answer
With what I have now gathered, I think this may be your best option.
convert sea.jpg \( -size 173x50 -background none label:"A" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
And this:
convert sea.jpg \( -size 173x50 -background none label:"A very much longer label" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
Basically, I am using some "aside processing" in parentheses to generate the text and then compositing it onto the page afterwards. I generate the text with label: to the best automatic size, then trim off any excess space around the text. I then centre the trimmed text, using -gravity center and expand the background out (using -extent) so that the text box is always the same size, then I can position it (with -geometry) when compositing it relative to the top-left corner as I reset -gravity to NorthWest.
Original Answer
If you want ImageMagick to do its best to fit your text in a given box, you should use caption rather than annotate, label or -draw "text".
So you want to load your sea image, set the size for the caption, draw the caption and composite that onto the image at the correct spot using -geometry:
convert sea.jpg -size 173x50! caption:"Your text" -geometry +312+66 -composite result.png
Or, with longer text:
convert sea.jpg -size 173x50! caption:"A considerably longer text that will result in a smaller font being chosen" -geometry +312+66 -composite result.png
If you want a blank background, use -background none before caption:
convert sea.jpg -size 173x50! -background none caption:"Your text" -geometry +312+66 -composite result.png
If you want to centre your text, you can do that with PANGO, like this, but I believe you then give up the auto-sizing feature:
convert sea.jpg -size 173x50! -background none -gravity center pango:'<span foreground="yellow">ABC</span>' -gravity northwest -geometry +312+66 -composite result.png

Related

Can't fit text to image with ImageMagick

I need to fit my text to image. My image have different sizes so i can't set constant pointsize.
My command looks something like this
convert
-fill white
-font Winter Calligraphy
-size `${options.width}x${options.height}`
label: KJHGFD
test.gif
on output you can see cropped part on top of picture.
Output:
I have problem only with this fonts, other fonts works great.
I tried to add white border on top. Unfortunately, this only moved the damaged text to bottom.
I can't change area size.
Text must fill as much space as possible.
I need to use Winter Calligraphy font
Here is a slightly kludgy way of getting the result you want. Here are the steps:
First, use caption: to get ImageMagick to tell you the pointsize it would use to fill your text box and extract that info
Create a canvas twice as wide and twice as tall as the one you really want and draw your text in the middle of that - it is bound to fit!
Now trim away the extraneous background around the text so you have the absolute minimum bounding box to contain the text
Resize the result to your desired size.
#!/bin/bash
# Width, height and text
w=600
h=150
text="KJHGFD"
# Get pointsize ImageMagick thinks is good
pointsize=$(convert -gravity center -background black -fill white -size ${w}x${h} \
-font "Winter Calligraphy.ttf" caption:"$text" -format "%[caption:pointsize]" info:)
echo ImageMagick likes pointsize: $pointsize
# So draw text in that size on larger canvas, trim to bounds of letters and resize to desired size
wb=$((w*2))
hb=$((h*2))
convert -gravity center -fill white -size ${wb}x${hb} xc:black \
-font "Winter Calligraphy.ttf" -pointsize $pointsize -annotate 0 "$text" \
-trim +repage -resize ${w}x${h}\! result.png
This works for me in ImageMagick 6.9.10.97 Q16 Mac OSX. I have added -background white -fill black -gravity center to your command.
convert -background white -fill black -font "/library/fonts/Winter Calligraphy.ttf" -size 569x196 -gravity center label:KJHGFD test.gif

Adding an overlay to og:image

I need to generate watermarked embed-image for Facebook in NodeJs. I noticed some websites' facebook posts have a banner on their image.
Using NodeJs, I'm wondering what would be the most elegant way to proceed.(ie. Something better than a photoshop action or a PHP script running elsewhere). I guess cropping the image to the right format and adding an overlay PNG with ImageMagik would be viable opion. Are you aware of online services providing this simple functionality?
Since this resource is rarely refreshed from FB, I would not store it on disk and generate it on the fly.
In ImageMagick, you can create a banner image using -annotate and composite over your image. For example starting with your image, suppose I want to put a similar banner at the top. I create a semi-transparent background color image of a desired width to match your image and desired height and pointsize. Then I composite it at the top of your image.
convert UT-copy-social.png.jpeg \
\( -size 1200x90 xc:"rgba(255,0,0,0.5)" -font Arial -pointsize 64 \
-fill white -gravity east -annotate +50+0 "TEST" \) \
-gravity north -compose over -composite result.jpg
If you want a semi-transparent gradient banner, then modify it as follows:
convert UT-copy-social.png.jpeg \
\( -size 90x1200 gradient:"rgba(255,0,0,0.5)-rgba(255,0,255,0.5)" \
-rotate 90 -font Arial -pointsize 64 -fill white \
-gravity east -annotate +50+0 "TEST" \) \
-gravity north -compose over -composite result2.jpg
You can also do it similarly with -draw or label:. See http://www.imagemagick.org/Usage/text/

Resize canvas of svg images in command line

I have bunch of svg images (1.svg 2.svg ...) and i would like to center them and resize canvas to the same value for all of them. Let's say that image 1.svg has w. 600 and h. 800 and 2.svg has w. 1000 and h. 400. I would like to set canvas for both as 1000x800 and center images. As the result the images won't be resized, but they will have extra space on sides -> 1.svg will still be 600x800 but in canvas with size 1000x800 and 2.svg will be 1000x400 but in canvas with size 1000x800. Is it possible to do in command line? Using inkscape, imagemagick etc.?
Thanks in advance!
You need the -extent option to ImageMagick, like this:
convert 1.svg -gravity center -background yellow -extent 1000x800 result.png
So, let's make some images:
convert -size 600x800! xc:red 1.png
convert 1.png -gravity center -background yellow -extent 1000x800 result.png
Or the other way:
convert -size 1000x400! xc:red 2.png
convert 2.png -gravity center -background blue -extent 1000x800 result.png

how to control the distorted and stretched image in graphicsmagick node.js

I want an image to be display by keeping its aspect ratio same by re-sizing it, but the image looks stretched. I have three images all of 50 by 50. I want the left image to be re-sized by maintaining its aspect ratio. The other two images will be shown up and down parallel to the first image. I think mosaic() build the four blocks. but I want three blocks, in fitst block first image will come up and it would be re-sized all the way till end, and the next two blocks the other two images will come up and down and show parallel to the first one. Below is the code
gm()
.in('-page', '+0+0')
.in('-resize', '50x100!')
.in('http://localhost:8080/image1')
.in('-page', '+50+0')
.in('http://localhost:8080/image2')
.in('-page', '+50+50')
.in('http://localhost:8080/image3')
.mosaic()
.write('C:/images/output.jpg', function (err) {
if (err) console.log(err);
});
I want an image like below but with keeping its aspect ratio, black image looks stretched, I don't want it be stretched, and black image should cover whole left area till end
If I remove ! the image then looks like, I want an image as above but not stretched
I don't use node but I think you will be able to adapt this:
Make our three images:
convert -size 50x50 -background black -fill white -gravity center -pointsize 36 label:"1" image1.jpg
convert -size 50x50 -background red -fill white -gravity center -pointsize 36 label:"2" image3.jpg
convert -size 50x50 -background red -fill white -gravity center -pointsize 36 label:"3" image3.jpg
Now resize and arrange:
convert image1.jpg -resize x100 -gravity center -extent 50x100 \( image2.jpg image3.jpg -append \) +append result.png
Essentially, I am making image1 100 pixels tall with -resize x100 and then extracting the central 50 pixel wide band down the middle with -gravity center -extent 50x100. Then, in parentheses and "on-the-side" I am loading image2 and image3 and placing one below the other with -append. That pair of images is then appended to the right of the original image with +append and the result is written out to result.png.

imagemagick text as a hole on solid background

I am simply trying to create some text as image with IM.
I want the size of the image to be automatically adjusted to text length.
Also I need the background color to be a solid specified color (rgb 166,127,000 in this example), and text color to be "transparent". I mean text "as hole" on the background.
I could obtain it with the following command:
/opt/local/bin/convert \( -font /fonts/myFont.ttf -pointsize 25 -background none -fill 'rgb(166,127,000)' -gravity center +antialias label:'TEST' \) -channel a -separate +channel -negate -fill -alpha set -channel RGBA -fill none -opaque black -fill 'rgb(166,127,000)' -opaque white png:-
I am looking for a simpler way to do it. Also I am having problems with antialias and the text looks too bulky.
Thank You.

Resources