align text to left with imagemagick - imagemagick

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

Related

Add top and left borders with magick command

Is there a way to add 5 or any number of white/transparent pixels at the top and left borders of an image with the magick command in Linux?
Use the -splice operator. First make a solid magenta rectangle:
magick -size 100x50 xc:magenta image.png
Now splice on a yellow chunk (so you can see it) 10 wide and 20 tall:
magick image.png -background yellow -gravity northwest -splice 10x20 result.png
Change yellow to none for transparent pixels.
Change magick to convert for v6 ImageMagick.
If you just want to splice to the East side:
magick image.png -background yellow -gravity east -splice 10x east.png
If you just want to splice to the South side:
magick image.png -background yellow -gravity south -splice x10 south.png

Position another image relatively to text with ImageMagick

convert inputImage.jpeg -gravity South -size x32 label:"Morning in paradise" -geometry +0+40 -composite starImage.png -composite finalImage.png
With this command, I can add text at the bottom of inputImage and another image on this text. But how can I set (or prefix) the starImage image to the left of the text that has a dynamic width and fixed height. I have attached some images below to explain what I want to do.
Obtained result
Expected result
You can read the star image, create the text label, and append them together inside parentheses. Then composite that assembled star-text image over the main input image. A command like this should get pretty near what you described.
convert inputImage.jpeg -gravity center -size x32 \
\( starImage.png label:"Morning in paradise" +append \) \
-geometry +0+40 -gravity South -composite finalImage.png
If you want a star on both sides of the line of text, you can read the "starImage.png" in once more after creating the label and before appending.
I think what you should do using Imagemagick is to set the width you want for the text so that there is room for the star image to be append on each side and have some padding as well. Here is how I would do it. Since you did not provide your input or star image, I have simulated the image as a blue image and taken some graphic image that I had around to simulate your star or logo. I first measure the desired width. The width is 70% of the difference is width between the large blue image and twice the width of the logo. I append the logo on each side of the text image, then composite that near the bottom of the blue image to create your final image. If this were in Imagemagick 7, it could be done in one command. This is Unix syntax.
Image:
Logo:
width=`convert background.jpg logo.png -format "%[fx:0.70*(u.w-2*v.w)]\n" info: | head -n1`
convert background.jpg \
\( logo.png \
-size ${width}x -background none -fill black -font Arial -gravity center label:"THIS IS A TEST" \
logo.png \
+append \) \
-gravity south -geometry +0+50 \
-compose over -composite \
result.jpg

Smooth text edge with ImageMagick

I'm using ImageMagick ver 6.9.3-7 to write text over pictures. I've noticed that the same text with the same font looks much better in html canvas and I'm trying to make it looks the same.
This is how it looks in the html5 canvas:
and this is how it's looks with IM:
(enlarge the images to view the different)
This is how I'm creating the image with node.js:
gmFrame
.font("./assets/impact.ttf", fontSize)
.stroke("#000")
.strokeWidth(8)
.draw(`gravity center text ${position.x},${position.y} '${text}'`)
.stroke("transparent")
.fill("#fff")
.draw(`gravity center text ${position.x},${position.y} '${text}'`);
Is there something to do about it?
After investigating the only way to do it is with blur.
This is an example for how to add text with blur without writing another file
convert -quality 100 "DJ Pauly D.jpg" -resize 500x500 \( +clone -alpha transparent -pointsize 52 -font impact -stroke "#000" -strokewidth 8 -draw "gravity center text 0,0 'WRITE SOMETHING'" -stroke "transparent" -fill "#fff" -draw "gravity center text 0,0 'WRITE SOMETHING'" -blur 0x5 \) -composite r2.png

Add gradient overlay under a photo label using imagemagick

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

Imagemagick label issue

I am using imagemagick 6.2.8 and i want to add a label at the bottom of the image aligned on the right side.
this is my code:
convert image.png -gravity center -background "#f0f0f0" -font bgothm.ttf -pointsize 18 label:"text text" -append "append_image.png"
however the label is aligned to the left
How can i align the label to the right?
Edit:
I found this: Before IM v6.4.7 it was much more difficult to align appended images, and generally involved using a "-flop" for right alignment. Or using "-extent" or "-border" to adjust the image width for centered aligned appends.
But i never used imagemagick
To align bottom-right, use -gravity southeast.
convert image.png -gravity southeast -background "#f0f0f0" -font bgothm.ttf -pointsize 18 label:"text text" -append "append_image.png"
Edit: I have updated my solution to work with your ImageMagick version.
Do the double -flop trick. One flop to mirror the text, so it will be right aligned and one flop to mirror the append to make it readable:
convert image.png -background "#f0f0f0" -font bgothm.ttf \
-pointsize 18 label:"text text" -flop -append -flop "append_image.png"
Produces this image for me. Please note that the order of -flop -append -flop do matter!
But still, a better solution would be to upgrade ImageMagick to >= 6.4.7 and use -gravity SouthEast:
convert image.png -gravity SouthEast -background "#f0f0f0" -font bgothm.ttf \
-pointsize 18 label:"text text" -append "append_image.png"

Resources