As per the documentation on Rmagick we can set the width of the rectangle within which our text is placed.
http://www.simplesystems.org/RMagick/doc/draw.html#annotate
Below is the code that we are using but we are unable to set the padding or a custom width for the text in the annotate block.
For a use case we wanted to have a custom padding for the text that we give in the annotate block.
img=Image.new(1500,600)
b=Magick::Draw.new
b.annotate(img,120,10,120,120,"5"){ |txt|
txt.pointsize = 58
txt.undercolor= "blue"
}
b.get_type_metrics(img, "5")
img.write("undercolor3.gif")
Have tried a ton number of things but couldn't make it work.Any ideas?
Draw a block of the desired undercolor on the image, then annotate the image such that the text is over the block.
To determine the desired size of the block, use get_type_metrics or get_multiline_type_metrics to get the dimensions of your text, then add in how much padding you want.
Annotate in ImageMagick does not have any option to pad the undercolor. But you can trick it. The simplest ways is to put spaces on the left and right side of your text using "\ ". But that will only pad on the left and right. In label: you can add newlines, but that does not work in annotate. You could add -interline-spacing, but that will only pad on the bottom. But here is a trick. Create text in the undercolor with a slightly larger pointsize, then write over it with your desired color text. For example:
Without padding:
convert logo.jpg -gravity center -undercolor pink -pointsize 24 -fill black -annotate +0+0 "This Is Some Text" result1.jpg
With padding:
convert logo.jpg -gravity center -pointsize 34 -undercolor pink -fill pink -annotate +0+0 "X X" -pointsize 24 -fill black -annotate +0+0 "This Is Some Text" result2.jpg
Alternately, you could just draw a pink rectangle in the center of the image of the desired size.
Related
I am using ImageMagick to add text to an image. I used to -gravity Center flag to put the text in the center of the image. But I think this is resulting in the text being center aligned too. I want the text at the center of the image but left aligned. Here is an example of what I'm trying to have:
This is the output I'm getting:
Current output
This is what I want:
This is my requirement
How do I accomplish this? This is my first time using ImageMagick. Please guide me.
Here is one way to do that in Imagemagick 6. I specify the background color, the font color (fill), the font and the pint-size and gravity west (left side). I use label: to create the two lines of text with a new line between them. This creates a text image of the size needed to hold the text. Then I pad the image all around that to the final size with the text image in its center using the same background color.
convert -background black -fill white -font ubuntu -pointsize 28 -gravity west label:"This is line 1 of text\nThis is line 2 of text" -gravity center -extent 400x300 result.png
See
https://imagemagick.org/Usage/text/
https://imagemagick.org/Usage/crop/#extent
ADDITION
If you want to put the text over an existing image, then you do something similar, but in place of the extent, we composite the text image over the background image.
The following is Unix syntax. For Window, remove the backslashes \ before the parentheses.
Input:
convert lena.png \( -background black -fill white -font ubuntu -pointsize 21 -gravity west label:"This is line 1 of text\nThis is line 2 of text" \) -gravity center -compose over -composite result.png
Result:
Or if you do not want the black background, use "none" for the color.
convert lena.png \( -background none -fill white -font ubuntu -pointsize 21 -gravity west label:"This is line 1 of text\nThis is line 2 of text" \) -gravity center -compose over -composite result2.png
I am building a image editor with php, jquery, image magic, I am trying to write text on image dynamic position but not getting the exact command of image magic.
I tried imagemagic commands of writing text but these commands are for fixed position. e.g i need to write text on image "hey love" on position (x: 831, y: 38) x is width and y is height now what i need is exact command able to write text on dynamic positions.
convert temp.jpg -gravity North -pointsize 30 -annotate +0+100 'Love you mom' temp1.jpg
this command uses gravity i have dynamic positions
You can do it like this:
convert -size 400x250 xc:cyan -pointsize 18 -fill magenta -draw "text 250,65 'Love you Mum'" result.png
I'm trying to generate an image using Imagemagick to match a preview in the browser, but the text comes out blurry. Does anybody have any suggestions? Attached is an image with the Imagemagick one on top, and browser one on bottom, along with the IM code.
convert -density 288 -resize 25% -background white -fill black -strokewidth 0 -stroke white -font Rubik-Regular.ttf -pointsize 10 -gravity center label:'This is a TEST!' label_arial.gif
You might find it easier to start with caption which automatically sizes the text the best way to fill a given area. So, as your lettering is around 140x36 pixels, you would do:
convert -size 140x36 -gravity center caption:'This is a TEST!' label.gif
I try to overlay two versions of the same text using different fonts (to create text effects like outlines and multi-color letters).
The fonts are defined with identical glyph widths, if I use Photoshop (or any text processing software) to generate the two texts and overlay them, they fit perfectly.
But ImageMagick doesn't seem to care about glyph widths but uses its own positioning system. So the two layers don't fit even if it's only a one-letter-text to be displayed.
What font definitions are used by ImageMagick?
Here my command for displaying a "4":
convert -size 500x500 xc:transparent -antialias -stroke none -pointsize 140 -gravity southwest -fill "#00ff00" -font FontBack.ttf -annotate 0x0+0+0 "4" -fill "#ff0000" -font FontOverlay.ttf -annotate 0x0+0+0 "4" -trim -virtual-pixel Background temp.png
To position the layers with the annotate options won't be a solution, as texts are generated dynamically in an imageprocessor.
Any ideas?
I'm trying to convert a bunch of photos using imagemagick. However, I hadn't figured out how to overlay an image with gradient and write some text on it. I know the text part though:
convert IMG_8408.jpg \
-font URWChanceryMediumI \
-pointsize 250 \
-draw "gravity south
fill black text 0,40 'Some text stuff here'" \
test.jpg
Is there a way to add a white gradient to the bottom? Note, that the image size may vary.
What I have:
What I want:
I picked the colors so that it's clearly visible what I want to achieve
You can achieve desired output with 3 commands:
a. create the upper part of your image (a solid rectangle with your selected background color):
convert -size 640x200 xc:#A02B2B background.jpg
b. create another image containing the text over a gradient:
convert -size 640x110 gradient:#A02B2B-#126B27 -pointsize 25 -draw "gravity south fill black text 0,40 'Some text stuff here'" text.jpg
c. combine the images to obtain the final output:
montage background.jpg text.jpg -tile 1x2 -geometry +0+0 output.jpg
Note: I modified text creation parameters in step 2 to keep the command short, but you can add back your original settings
Use the following command:
magick -size 640x310 -define gradient:vector="0,107 0,0" gradient:"#a02b2b-#126b27" -flip -gravity south -font script-mt-bold -pointsize 48 -annotate +0+24 "Some text stuff here" output.png